Page 223 - DCAP201_FUNDAMENTALS_OF_DATA_STRUCTURES
P. 223

Fundamentals of Data Structures




                    Notes                  cout << “Unsorted list:” << endl;                //  Displaying the
                                   unsorted array
                                           for(int i=0; i<n; i++)
                                           {
                                                   cout << a[i] << “ ”;
                                           }
                                           for (int i=n-1;i>1;—i)
                                           {
                                                   int locmax=0;
                                                   int maxtemp=a[0];
                                                   for (int j=1; j<=i; ++j)
                                                   {
                                                           if(a[j]>maxtemp)
                                                           {
                                                                   locmax=j;
                                                                   maxtemp=a[j];
                                                           }
                                                   }
                                                   a[locmax]=a[i];
                                                   a[i]=maxtemp;
                                           }
                                           cout << “Sorted list:” << endl; // Displaying the sorted array
                                           for(int i=0; i<n; i++)
                                           {
                                                   cout << a[i] << “ “;
                                           }
                                            return 0;
                                   }
                                   The output is shown as below:










                                   The algorithm executes n–1 iterations always, no matter if the correct order was achieved earlier
                                   or the array was already sorted.
                                   The first element is retained in the maxtemp variable, then the next element is checked with
                                   maxtemp to see which is greater, and if this is true, maxtemp retains the next big value and so on.
                                   After this has been completed, on the last position, there will be the greatest element from the
                                   list, so the greatest element ends on the right position after the first run.
                                   This code example of the algorithm sorts the array from greater to lower, as opposing to the step
                                   by step example that I gave earlier.




          216                               LOVELY PROFESSIONAL UNIVERSITY
   218   219   220   221   222   223   224   225   226   227   228