Hi all. basically I need to hand up a project on merge and sort array in char.
First input: a c e g
Second input: b d f h
Output: a b c d e f g h
I found this code using bubble sort. But i don't understand some of the line mean. so can someone explain out for me? or is there any easy way i can code myself without using bubble sort?
Code:#include<stdio.h> #include<stdlib.h> void sort(char data[], int length) { int end = length - 1; for (int i = 0; i < length; ++i) { for (int j = 0; j < end; ++j) { if (data[j] > data[j+1]) { char tmp = data[j]; data[j] = data[j+1]; data[j+1] = tmp; } } --end; } } int main(void) { char firstArray[100]; char secondArray[100]; char mergedArray[200]; int i; int mergedIndex; printf("Put in the first array\n"); fgets(firstArray, sizeof(firstArray), stdin); printf("Put in the second array\n"); fgets(secondArray, sizeof(secondArray), stdin); // Copy the first array into the merged array for(i = 0; firstArray[i] != '\n'; ++i) { mergedArray[i] = firstArray[i]; } mergedIndex = i; for(i = 0; secondArray[i] != '\n'; ++i) { mergedArray[mergedIndex++] = secondArray[i]; } mergedArray[mergedIndex] = 0; sort(mergedArray, mergedIndex); printf("Merged array is '%s'\n", mergedArray); system("pause"); return 0; }



LinkBack URL
About LinkBacks





But I'm looking for the simple method to code out if i can. 