You are on page 1of 42

Week 14

Lecture 14

Sorting

Compiled by
G. N. Pachshenko
Definition:
A sorting algorithm is an algorithm that puts
elements of a list in a certain order.
Unsorted array:
Sorted array:
It is often necessary to arrange the elements in
an array in numerical order

from highest to lowest values


(descending order)
12 8 5 3 -1

or vice versa (ascending order). 


-8 -2 0 3 7 25 48
The process of sorting an array requires the
exchanging of values.
grade[1] = grade[2];
grade[2] = grade[1];      // DOES NOT WORK!!!
Temporary holding variable
In order to swap two values, you must use
a third variable,
(a "temporary holding variable"), to temporarily
 hold the value you do not want to lose.
temp = grade[1];     
grade[1] = grade[2];
grade[2] = temp;
There are hundreds of different ways to sort
arrays.

The basic goal of each of these methods is the


same:  to compare each array element to
another array element and swap them if they
are in the wrong position. 
Popular sorting algorithms

• Simple sorts
– Insertion sort
– Selection sort
• Efficient sorts
– Merge sort
– Heapsort
– Quicksort
• Bubble sort and variants
– Bubble sort
– Shell sort (Shell is a surname)
– Comb sort
• Distribution sort
– Counting sort
– Bucket sort
– Radix sort
 
Bubble Sort
In the bubble sort, as elements are sorted they
gradually "bubble" (or rise) to their proper
location in the array, like bubbles rising in a glass
of soda.  The bubble sort repeatedly
compares adjacent elements of an array.
The first and second elements are compared and
swapped if out of order. 
Then the second and third elements are compared
and swapped if out of order. 

This sorting process continues until the last two


elements of the array are compared and swapped if
out of order. 
 
When this first pass through the array is
complete, the bubble sort returns to elements
one and two and starts the process all over
again. 
 When does it stop?  
The bubble sort knows that it is finished when
it examines the entire array and no "swaps" are
needed (thus the list is in proper order). 
 Bubble sort for descending order
Array at beginning:  84 69 76 86 94 91

After Pass #1: 84 76 86 94 91 69

After Pass #2:  84 86 94 91 76 69

After Pass #3:  86 94 91 84 76 69

After Pass #4: 94 91 86 84 76 69

After Pass #5 (done):  94 91 86 84 76 69


A "pass" is defined as one full trip through the
array comparing and if necessary,
swapping, adjacent elements.  
Pass #1
84 69 76 86 94 91
84 69 76 86 94 91
84 76 69 86 94 91
84 76 69 86 94 91
84 76 86 69 94 91
84 76 86 69 94 91
84 76 86 94 69 91
84 76 86 94 69 91
84 76 86 94 91 69
With a bubble sort, it is always necessary to
make one final "pass" through the array to
check to see that no swaps are made to ensure
that the process is finished. 
Bubble Sort Function for Descending Order 
#include <iostream>
using namespace std;

int main()
{
int num[5];
int i, j, flag = 1; // set flag to 1 to start first pass
int temp; // holding variable

for (i = 0; i < 5; i++)


{cin >> num[i]; }

for (i = 1; (i <5) && flag; i++)


{
flag = 0;
for (j = 0; j < (5 - 1); j++)
{
if (num[j + 1] > num[j]) // ascending order simply changes to <
{
temp = num[j]; // swap elements
num[j] = num[j + 1];
num[j + 1] = temp;
flag = 1; // indicates that a swap occurred.
}
}
}
for (i = 0; i < 5; i++)
{
cout<< num[i]<< " ";
}

return 0;
} // to demonstrate it
Exchange Sort
The exchange sort is similar to the bubble sort,
in that it compares elements of the array and
swaps those that are not in their proper
positions. 
The difference between these two sorts is the
manner in which they compare the elements. 

The exchange sort compares the first element


(on the first position; may change the value during one pass)
with each following element of the array,
making any necessary swaps. 
 
When the first pass through the array is
complete, the exchange sort then takes
the second element and compares it with
each following element of the array
swapping elements that are out of order. 
This sorting process continues until the
entire array is ordered. 
Let's examine our same table of elements again
using an exchange sort for descending order. 
Remember, a "pass" is defined as one full trip
through the array comparing and if necessary,
swapping elements.
Array at beginning:  84 69 76 86 94 91

After Pass #1: 94 69 76 84 86 91

After Pass #2: 94 91 69 76 84 86

After Pass #3:  94 91 86 69 76 84

After Pass #4:  94 91 86 84 69 76

After Pass #5 (done):  94 91 86 84 76 69


Pass #1
84 69 76 86 94 91
84 69 76 86 94 91
84 69 76 86 94 91
86 69 76 84 94 91
86 69 76 84 94 91
94 69 76 84 86 91
94 69 76 84 86 91
• The exchange sort, in some situations, is
slightly more efficient than the bubble sort. 

• It is not necessary for the exchange sort to


make that final complete pass needed by the
bubble sort to determine that it is finished.
 for (i=0; i< (n -1); i++)    // element to be compared
    {
          for(j = (i+1); j < n; j++)   // rest of the elements
         {
                if (num[i] < num[j])          // descending order
               {
                        temp= num[i];          // swap
                        num[i] = num[j];
                        num[j] = temp;
               }
          }
     }
Selection Sort
The algorithm divides the input list into two parts:
the sublist of items already sorted, which is built up
from left to right at the front (left) of the list, and
the sublist of items remaining to be sorted that
occupy the rest of the list.
Initially, the sorted sublist is empty and the
unsorted sublist is the entire input list. The
algorithm proceeds by finding the smallest (or
largest, depending on sorting order) element in the
unsorted sublist, exchanging (swapping) it with the
leftmost unsorted element (putting it in sorted
order), and moving the sublist boundaries one
element to the right.
example
64 25 12 22 11 // this is the initial, starting state
of the array
11 25 12 22 64 // sorted sublist = {11}
11 12 25 22 64 // sorted sublist = {11, 12}
11 12 22 25 64 // sorted sublist = {11, 12, 22}
11 12 22 25 64 // sorted sublist = {11, 12, 22,
25}
11 12 22 25 64 // sorted sublist = {11, 12, 22, 25,
64}
Nothing appears changed on these last two lines
because the last two numbers were already in
order.
While being an easy sort to program, the
selection sort is one of the least efficient. 

The algorithm offers no way to end the sort


early, even if it begins with an already sorted
list. 
Selection Sort Function for Descending
Order
 for (i= n - 1; i > 0; i--)
     {
           first = 0;                 // initialize to subscript of first element
           for (j=1; j<=i; j++)   // locate smallest between positions 1 and i.
          {
                 if (num[j] < num[first])
                 first = j;
          }
         temp = num[first];   // Swap smallest found with element in position i.

         num[first] = num[i];


         num[i] = temp;
     }
    
#include <iostream>
using namespace std;

int main()
{
int num[5];
int i, j, flag = 1;
int temp, first;
int n = 5;

for (i = 0; i < 5; i++)


{
cin >> num[i];
}

for (i = n - 1; i > 0; i--)


{
first = 0;
for (j = 1; j <= i; j++)
{
if (num[j] < num[first])
first = j;
}
temp = num[first]; // swap elements
num[first] = num[i];
num[i] = temp;
}

for (i = 0; i < 5; i++)


{
cout << num[i] << " ";
}

return 0;
}
For array of 5 elements:
i=4 j=1
i=4 j=2
i=4 j=3
i=4 j=4
i=3 j=1
i=3 j=2
i=3 j=3
i=2 j=1
i=2 j=2
i=1 j=1
insertion sort
The insertion sort works in a slightly different
way. It always maintains a sorted sublist in the
lower positions of the list. Each new item is then
“inserted” back into the previous sublist such
that the sorted sublist is one item larger. 
 for(j = 1; j < n; j++)    // Start with 1 (not 0)
    {
           key = num[j];
           for(i = j - 1; (i >= 0) && (num[i] < key); i--) 
  // Smaller values move up
          {
                 num[i+1] = num[i];
          }
         num[i+1] = key;  
  //Put key into its proper location
     }

You might also like