Hello,
I need help with re-ordering the values in ascending order in the array (Unordered). However, I cannot use swap or sort function (i.e. Bubblesort).
How can I do that?
Hello,
I need help with re-ordering the values in ascending order in the array (Unordered). However, I cannot use swap or sort function (i.e. Bubblesort).
How can I do that?
You might want to elaborate. Why is it that you "cannot use swap or sort function (i.e. Bubblesort)"? Taking that restriction literally, the simple solution is to use qsort(), since it is neither a swap function nor is it likely to be an implementation of bubblesort.Quote:
Originally Posted by fedya
I think you've asked the same question few days ago here and Adak has beautifully explained it.
You can't. As I told you last time it's a logical contradiction to have some code that sorts something (which itself is therefore a sorting algorithm) and to also not use a sorting algorithm.
It's like asking us how you can run a running race without moving your legs.
You just don't seem to be learning what's wrong with your question.
Thanks Ben10.
This is another example for you, fedya.
Code:/* IndxSort.c by Adak. A small example of sorting through an index
status: ok
*/
#include <stdio.h>
#define NumItems 4
#define StringLen 30
int main() {
int i, j, temp;
int Index[NumItems];
//make 2 arrays, (which work in parallel). One for strings, and one for the int
int Int[NumItems] = { 7, 2, 11, 6 };
//and a few lines from The Rhyme of the Ancient Mariner:
char Strings[NumItems][StringLen] = {
{ "Water, water, everywhere" },
{ "And all the boards did shrink" },
{ "Water, water, everywhere" },
{ "Nor any drop to drink" }
};
//initialize the index array
for(i = 0; i < NumItems; i++)
Index[i] = i;
//now a simple sort, using the initialized index array and the Int array
for(i = 0; i < NumItems - 1; i++) {
for(j = i + 1; j < NumItems; j++) {
if(Int[Index[i]] > Int[Index[j]] ) {
temp = Index[i];
Index[i] = Index[j];
Index[j] = temp;
}
}
}
//Now to see the sorted data, using the Index array
printf("\n\n\n Int and String data, sorted by the index Int[]:\n\n");
for(i = 0; i < NumItems; i++)
printf("Int: %2d String: %s \n", Int[Index[i]], Strings[Index[i]]);
printf("\n\n\n Original Int and String data:\n\n");
//to see the unsorted data
for(i = 0; i < NumItems; i++)
printf("Int: %2d String: %s \n", Int[i], Strings[i]);
printf("\n\n\t\t\t press enter when ready");
i = getchar();
return 0;
}