• home
  • forum
  • my
  • kt
  • download
  • Selection Sort

    Author: 2009-04-23 09:15:59 From:

    This is one of the simpliest algorithms for sorting... it is not recommended to use it with many numbers, as it will take long probably... it is good for sorting about 20-30 numbers approximately...


    Selection Sort


    This algorithm is very simple in fact! Here is how it works...

    Imagine that you have these numbers:
    10 32 4 43 12 65
    which you want to sort...

    This algorithm first finds the lowest number and puts it on first place. In this example, the lowest number is 4. So, after fining it and putting it on first place, the numbers should look like this:
    4 32 10 43 12 65

    Notice that on the place where the "4" was, it's now the first number...

    Now, continuing in this order, the algorithm should find the lowest number from these:
    32 10 43 12 65 /the line still has the "4", but it is already sorted so I won't write it here.../
    We exclude "4" as we already found that it is the lowest in the previous case.
    So, in the new search, the lowest number is 10, so our line should look like this:
    10 32 43 12 65

    After that you should find the lowest number from these:
    32 43 12 65
    It is- 12, so the new look is:
    12 43 32 65

    The next step is to find the lowest number from these:
    43 32 65
    It is- 32, so the new look is:
    32 43 65

    Now, we have only two numbers left, to compare- 43 and 65. We see that 43 is lower than 65, so it keeps this order.
    So, after all, we have this:
    4 10 12 32 43 65


    Here is a sample C++ code that makes this sorting. The code is written by me. It uses a function that takes parameters, so you can implement it in other programs, too...


    #include <iostream.h>

    //Selection Sort function
    //Takes two parameters:
    //int *array- the array with the numbers to be sorted
    //int size- the size of the array
    void selsort(int *array,int size)
    {
    int min;
    int b;

    //This loop goes through the whole array
    for(int a=0;a<size-1;a++)
    {
      b=a;
      min=array[b]; //Get the current value

      //...and check if any of the rest numbers is lower
      for(int j=a+1;j<size;j++)
      {
       if(array[j]<min)
       {
        b=j; min=array[b]; //...and if yes, then get it
       }
      }

      //Switch the values...
      array[b]=array[a];
      array[a]=min;

    }
    }

    //Main program function
    void main()
    {
    beginning:
    const int maxsize=100;
    int count;
    char answer;

    cout << "How many numbers you want to sort?: ";
    cin>> count;
    if(count>maxsize)
    {
      cout<<"Too many..."<<endl;
      cout<<"Your limit is: "<<maxsize<<endl;
      cout<<"Want to try again? (y/n): ";
      cin>>answer;
      if(answer=='y')
       goto beginning;
      else
       cout<<endl<<"Bye bye...!"<<endl;
    }
    else
    {
      int *numbers;
      numbers=new int[count];

      for(int y=0;y<count;y++)
      {
       cout<<"Enter number #"<<y+1<<" : ";
       cin>>numbers[y];
      }
      
      selsort(numbers,count); //Sort the numbers...

      cout<<endl<<endl;

      //Print the results...
      cout<<"Result: "<<endl;
       for(int h=0;h<count;h++)
       cout << numbers[h]<<" ";

      cout<<endl;

      //Free the memory...
      delete [] numbers;
    }
    }

    discuss this topic to forum

    relation tutorial

    No information

    Category

      Database Related (0)
      Development (5)
      File Manipulation (3)
      Games and Entertainment (10)
      Graphics (15)
      Introduction to C and Cpp (29)
      Miscellaneous (12)
      Networking (5)
      Programming in C and Cpp (10)
      Security (3)

    New

    Hot