Thread: Help with a data sorting algorithm

  1. #1
    Registered User mlupo's Avatar
    Join Date
    Oct 2001
    Posts
    72

    Help with a data sorting algorithm

    I've worked for two days on this algorithm but I'm stumped.
    It's not printing out what the instructor states.
    Where am I going wrong?

    Thanks in advance,
    Mike

    /*Arrange the following single dimension array in column major order. (Yes, the entire array, not just each column separately)

    4 5 6
    4 5 6
    7 8 9
    10 11 12
    7 8 9

    RULES: DO NOT use printf in some way to make it print in the correct order.
    Instead, Move the items in the linear array to make this so.
    if printed linearly it should print:
    4,6,9,4,7,9,5,7,10,5,8,11,6,8,12.
    */
    Code:
    #include<stdio.h>
    
    
    int main(){
      
      int array[15]={4,5,6,4,5,6,7,8,9,10,11,12,7,8,9};
      int i,top,temp;
      for(top=0; top<=14; top=top+1){
        for(i=top+1; i<=14; i++){
          if(array[top]>array[i]){
            temp=array[top];
            array[top]=array[i];
            array[i]=temp;
            break;
          }
          if(array[top]==array[i] && (top+1)<14 && (top!=i && (top+1)!=i)){
            temp=array[top+1];
            array[top+1]=array[i];
            array[i]=temp;
            break;
          }
        }
      }
    
      for(i=0; i<15; i++)
        printf("%d,", array[i]);
    
    
      return 1;
    }
    NEVER PET YOUR DOG WHILE IT'S ON FIRE!

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    How is this sorting? There is no logical order to the end result.

    [edit]
    4,5,6
    4,5,6
    7,8,9
    10,11,12
    7,8,9

    4,6,9
    4,7,9
    5,7,10
    5,8,11
    6,8,12

    Gotcha. It helps if you display the output correctly. Sort "columns" of every third place. Thus, when you're looping through to sort, increment or decrement in steps of three.

    Personally, I'd probably design a function that took the five arguments and returned a pointer to a static array of them. Then I'd overwrite their places in line with the contents of the array.
    [/edit]

    Quzah.
    Last edited by quzah; 10-31-2002 at 09:26 PM.
    Hope is the first step on the road to disappointment.

  3. #3
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    If the number-example of quzah is correct, then you could perform these steps:

    1. Put all elements in a linear list.

    Original array:

    4,5,6
    4,5,6
    7,8,9
    10,11,12
    7,8,9

    Gives list:

    [4, 5, 6, 4, 5, 6, 7, 8, 9, 7, 8, 9, 10, 11, 12, 7, 8, 9]

    2. Sort the linear list.

    Sorting gives:

    [4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 11, 12]

    Indexed as:

    [n1, n2, n3, n4, n5, n6, n7, n8, n9, n10, n11, n12]

    3. Rearrange the list:

    Output should be:

    4,6,9
    4,7,9
    5,7,10
    5,8,11
    6,8,12

    Which is:

    n1 n5 n9
    n2 n6 n10
    n3 n7 n11
    n4 n8 n12

    Rearranging can be done easily. Put the first N characters in the linear list in the first colum, the second N characters in the second colum. Here N is the number of rows in the original matrix.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bitmasking Problem
    By mike_g in forum C++ Programming
    Replies: 13
    Last Post: 11-08-2007, 12:24 AM
  2. Merge Sort the best sorting algorithm?
    By sehr alt in forum A Brief History of Cprogramming.com
    Replies: 21
    Last Post: 06-20-2007, 02:19 PM
  3. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM
  4. C diamonds and perls :°)
    By Carlos in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 05-16-2003, 10:19 PM
  5. C Programming Question
    By TK in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 07-04-2002, 07:11 PM