Untitled
unknown
plain_text
a year ago
2.7 kB
5
Indexable
Never
#include <stdio.h> void f2(int a[], int n); void f3(int a[], int n); int f4(int a[], int n); int main() { void f2(); void f3(); int f4(); } void f2(int a[], int n) { int count[4] = { 0 }; // An array to keep track of how many times each number appears in the array // Counting the occurrences of each number in the array for (int i = 0; i < n; i++) { count[a[i]]++; } // Filling the array in the appropriate order int index = 0; for (int j = 0; j <= 3; j++) { for (int k = 0; k < count[j]; k++) { a[index++] = j; } } } void f3(int a[], int n) // This function takes an array and its length as input parameters { int i, j, temp; // Declare three integer variables i, j, and temp // First pass: move all 0s to the front of the array j = 0; // Set j to 0 to keep track of the index of the first non-zero element for (i = 0; i < n; i++) { // Iterate over the array if (a[i] == 0) { // If the element is 0 temp = a[i]; // Swap the element with the first non-zero element a[i] = a[j]; a[j] = temp; j++; // Move the index of the first non-zero element } } // Second pass: move all 1s after the 0s for (i = j; i < n; i++) { // Iterate over the remaining elements in the array if (a[i] == 1) { // If the element is 1 temp = a[i]; // Swap the element with the first non-one element a[i] = a[j]; a[j] = temp; j++; // Move the index of the first non-one element } } // Third pass: move all 2s after the 1s for (i = j; i < n; i++) { // Iterate over the remaining elements in the array if (a[i] == 2) { // If the element is 2 temp = a[i]; // Swap the element with the first non-two element a[i] = a[j]; a[j] = temp; j++; // Move the index of the first non-two element } } } int f4(int a[], int n) { int i, j, temp; // Sort the array a in ascending order using bubble sort algorithm for (i = 0; i < n - 1; i++) { for (j = 0; j < n - i - 1; j++) { if (a[j] > a[j + 1]) { temp = a[j]; a[j] = a[j + 1]; a[j + 1] = temp; } } } // Check if the sorted array contains a continuous sequence of numbers for (i = 1; i < n; i++) { if (a[i] != a[i - 1] + 1) { return 0; // if not, return 0 } } return 1; // if yes, return 1 }