Thread: What is this sorting technique?

  1. #1
    Registered User
    Join Date
    Feb 2016
    Posts
    27

    What is this sorting technique?

    I have an
    Code:
     array[4] =  {5, 3, 9, 1}
    I compare the first index with the second and if the second index is smaller then I'll swap the first index with the second.
    After 1st pass I get:
    Code:
      {3, 5, 9, 1}
    I then compare the first index with the third index and if the third index is smaller then I'll swap. In this case it isn't.
    I'll then compare the first index with the fourth index and if the fourth index is smaller I'll swap it with the first index.
    After this pass I get:
    Code:
     {1, 5, 9, 3}
    I will then compare the second index with every index that follows so I'll get:
    Code:
     {1, 3, 9, 5}
    then

    Code:
      {1, 3, 5, 9}
    Can someone tell me the name of this sorting algorithm?
    I'm trying to do some research on it but I don't have a name for it.

  2. #2
    Registered User
    Join Date
    Mar 2016
    Posts
    203
    Quote Originally Posted by Amrita Ramnauth View Post
    I then compare the first index with the third index and if the third index is smaller then I'll swap.
    It should be "I then compare the original first index with the third index" since '5' (in your example) is now actually the second index after the first swap and so on. So I think you're trying to arrange the numbers in ascending order. Search by this term and it might be what you're looking for.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    596
    That sounds like a Selection Sort:

    https://en.wikipedia.org/wiki/Selection_sort

  4. #4
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Is this the algorithm you mean?
    Code:
    void mystery_sort(int a[], int n) {
        for (int i = 0; i < n - 1; i++)
            for (int j = i + 1; j < n; j++)
                if (a[i] > a[j]) {
                    int t = a[i];
                    a[i] = a[j];
                    a[j] = t; 
                }
    }
    I also think it's a kind of selection sort, but an inefficient one that does too many swaps. In a normal selection sort you find the smallest element and swap it into the first position, then find the next smallest and swap it into the second position, etc. In your sort you swap every time you find a smaller element than the one currently in the first position, etc. The effect is the same in that the smallest element is moved to the first position, then the second smallest to the second position, etc.

    A more usual selection sort:
    Code:
    void selection_sort(int a[], int n) {
        for (int i = 0; i < n - 1; i++) {
            int k = i;
            for (int j = i + 1; j < n; j++)
                if (a[j] < a[k])
                    k = j;
            int t = a[i];
            a[i] = a[k];
            a[k] = t; 
        }
    }
    Obviously selection_sort will be faster than mystery_sort since it implements essentially the same algorithm but does fewer swaps.

  5. #5
    Registered User
    Join Date
    Feb 2016
    Posts
    27
    Yes it's like the selection sort but it's not as efficient as the selection sort. And the algorithm you posted for it is correct.
    I just wanted to know if that sort exists and what's the name of it if you know.
    I'm doing a comparison of it to the selection sort.

  6. #6
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Quote Originally Posted by Amrita Ramnauth View Post
    Yes it's like the selection sort but it's not as efficient as the selection sort. And the algorithm you posted for it is correct.
    I just wanted to know if that sort exists and what's the name of it if you know.
    I'm doing a comparison of it to the selection sort.
    If it has a name it would be something like "stupid selection sort".

  7. #7
    Registered User
    Join Date
    Feb 2016
    Posts
    27
    I'm trying to sort 500k random numbers in my codeblocks compiler which I call from a .txt file. When I run the program it just gives me a blinking pointer to the top and does nothing. Any idea of what's happening there?

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Try to sort 500 random numbers instead. Alternatively, add output to track the progress, but that could slow down your program further.
    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

  9. #9
    Registered User
    Join Date
    Feb 2016
    Posts
    27
    I can get to sort up to 100,000

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Well, then it probably works for 500K random numbers, except that it is excruciatingly slow with that size of input because it doesn't have a particularly favourable time complexity.
    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

  11. #11
    Registered User
    Join Date
    Feb 2016
    Posts
    27
    Hmm I see
    Thanks

  12. #12
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    For me it takes about 10 seconds for 100000 elements and 40 seconds for 200000. This is nicely in line with the quadratic time complexity: double the number of elements, four times the time. So I predict 500000 would take about 250 seconds (4 minutes and 10 seconds).

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    A word on terminology though:
    Quote Originally Posted by Amrita Ramnauth
    I'm trying to sort 500k random numbers in my codeblocks compiler which I call from a .txt file.
    By the above sentence, I understood that you compiled your program with the compiler configured for your Code Blocks IDE, and then you ran this program via the Code Blocks IDE, which then read the 500K random numbers from a .txt file.

    It is not terribly important right now, but you should be aware at some point that Code Blocks is an "Integrated Development Environment", not a compiler. It integrates a compiler, e.g., gcc. The phrase "trying to sort 500k random numbers in my codeblocks compiler" does not actually make sense even when we replace "compiler" with "IDE" because your program is not your IDE. You didn't call your program from a .txt file -- that would mean that your .txt file is executable, which is not conventional -- but rather your program read the .txt file.
    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

  14. #14
    Registered User
    Join Date
    Feb 2016
    Posts
    27
    Yes the program reads the .txt file
    That's what I meant
    My bad

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My Programming Technique
    By User-_-Name in forum C++ Programming
    Replies: 1
    Last Post: 01-15-2011, 11:10 PM
  2. a more advanced technique
    By Yarin in forum C++ Programming
    Replies: 53
    Last Post: 08-07-2008, 04:57 AM
  3. Compression Technique
    By vikranth in forum C Programming
    Replies: 9
    Last Post: 11-06-2007, 09:43 AM
  4. Debugging Technique
    By WaltP in forum Tech Board
    Replies: 4
    Last Post: 05-26-2006, 08:35 AM
  5. unknown technique
    By JagWire in forum Windows Programming
    Replies: 4
    Last Post: 07-06-2002, 04:21 PM

Tags for this Thread