Hello

i have a a struct which contains the below

1
10
11
2
3

But i need this to be in numeric order, ie

1
2
3
10
11

below is my code

do {
    fprintf(fReport, "List Name = %d\n", LIST[t].name );
    t++;
 
 } while( t != k )

How can i bring them back in numeric order?

You will have to sort all the information you want to print out before you print the information.

Obtain all items to print.
Sort items to print.
Print items.

The process that does the sorting can be any of a number of protocols including bubble sort, quick sort, insert sort, the sort algorhythms from the Standard Template Library, etc. Which sorting protocol is best for you depends on your knowledge base, how many items you have to sort, how random the original series is, etc, etc.

Wikipedia has a whole page dedicated to sorting algorithms:

http://en.wikipedia.org/wiki/Sorting_algorithm

Snippet taken from the Dutch wikipedia, "invoer" is the array you want to sort and lengte is the number of values in the array

void bubblesort(int invoer[],int lengte){
  int i,j,tijdelijk;
  for(j=0;j<lengte-1;j++){
    for(i=1;i<lengte-j;i++){
      if(invoer[i-1]>invoer[i]){
        tijdelijk=invoer[i];
        invoer[i]=invoer[i-1];
        invoer[i-1]=tijdelijk;
      }
    }
  }
}

note that this is a really in-efficient way of sorting. I once executed it on a 14000 number array. Took several minutes.

this:

void straightselection(int invoer[],int lengte){
  int i,j,kleinste,tijdelijk;
  for(j=0;j<lengte-1;j++){
    kleinste=j;
    for(i=j+1;i<lengte;i++){
      if(invoer[i]<invoer[kleinste]) kleinste=i;
    }
    if(kleinste!=j){
      tijdelijk=invoer[j];
      invoer[j]=invoer[kleinste];
      invoer[kleinste]=tijdelijk;
    }
  }
}

is also taken from the Dutch wikipedia. It is a straight selection sort algorithm. It looks for the smallest item in the array and puts it on the end of the array. You might want to improve this algorithm by letting it search for the smallest and the largest at the same time.


Goodluck,
Eddy

If u want the fastest algorithm performance wise, quicksort is one of them. Even though its a recursive algorithm but still does the trick nonetheless considering the ulimited size of stack.

If u want the completer source code maybe this would help
http://linux.wku.edu/~lamonml/algor/sort/quick.html

If the list to sort will only consist of integers and you know the smallest and the largest value you can also use counting sort. Rather fast, very easy.


Greetz, Eddy

You can use the standard C function qsort(), which, despite its name, doesn't have to use a quicksort algorithm, just a sorting algorithm.

If you want to just sort a couple of numbers then you could use the bubble sort (slow but simple). (Google for "bubble sort".)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.