Thread: Sort values in an array without using sort functions (Urgent)

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    3

    Sort values in an array without using sort functions (Urgent)

    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?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by fedya
    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).
    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 Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by fedya View Post
    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?
    I think you've asked the same question few days ago here and Adak has beautifully explained it.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by fedya View Post
    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 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.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by BEN10 View Post
    I think you've asked the same question few days ago here and Adak has beautifully explained it.
    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;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. help with functions and array please
    By rebel in forum C++ Programming
    Replies: 4
    Last Post: 11-24-2005, 01:09 PM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Replies: 2
    Last Post: 11-22-2001, 12:22 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM