It's really quite simple. Assuming you know how to code a bubble sort, let's say these are some fragments of your number-handling code:
Code:
int array[ELEMENTS]; /* declare the number array */
scanf("%d", &array[x]); /* read in a number */
printf("%d\n", array[x]); /* print a number */
if(array[x] > array[x+1])) { /* compare two numbers (part of bubble sort) */
array[x] = temp; /* assign to a number (part of swap) */
void function(int array[]) { /* pass array to function */
The corresponding string-handling code would look like this:
Code:
char array[ELEMENTS][MAX_STRING_LENGTH]; /* declare the string array */
scanf("%s", array[x]); /* read in a string */
printf("%s\n", array[x]); /* print a string */
if(strcmp(array[x], array[x+1]) > 0) { /* compare two strings (part of bubble sort) */
strcpy(array[x], temp); /* assign to a string (part of swap) */
void function(char array[ELEMENTS][]) { /* pass array to function */
I said, "assuming you know how to code a bubble sort". If you don't, have a look at some online tutorials about bubble sorts.
http://www.cprogramming.com/tutorial.../sorting1.html
http://en.wikipedia.org/wiki/Bubble_sort
Or have a look at this:
Code:
void sort_numbers(int array[], size_t elements) {
size_t x = 0;
int temp, done = 0;
while(!done) {
done = 1;
for(x = 0; x + 1 < elements; x ++) {
if(array[x] > array[x+1]) {
/* swap array[x] and array[x+1] */
temp = array[x];
array[x] = array[x+1];
array[x+1] = temp;
}
}
}
}
You can use int instead of size_t if you want -- I just like using variables of type size_t for indexing arrays.
You mentioned qsort(). qsort() is a C library function that performs a quick sort (usually) -- much more efficient than a bubble sort, but not as efficient as a quick sort could be, because qsort() has to be generic. (Quick sort is generally regarded as one of the fastest sorting algorithms.)
I say usually because, despite its name, qsort() can use any sorting algorithm. Most implementations do use quick sort, however. Not that you, the programmer, care. As long as it gets sorted, who cares how it does it?
To use quick sort, you need to create a function that will tell qsort() if the first of two elements is less than, greater than, or equal to the second. Here's an example for numbers: http://www.cplusplus.com/reference/c...lib/qsort.html
Here's one that's a bit easier to understand:
Code:
int compare(const void *a, const void *b) {
int x = *(int *)a, y = *(int *)b;
if(x < y) return -1;
else if(x > y) return 1;
else return 0; /* x == y */
}
For strings, you can get away with
Code:
int compare(const void *a, const void *b) {
return strcmp(a, b);
}
because strcmp() does just what qsort is looking for.
Then you call qsort like this:
Code:
int n, values[] = {1, 2, 3, 4, 5, 6};
qsort(values, sizeof(values)/sizeof(*values), sizeof(int), compare);
(Instead of sizeof(values)/sizeof(*values), you could use 6, I suppose. )
That "compare" argument is the address of the function compare(). Never mind how it works, just put the name of your function in there. &compare and compare work equally well.