University Institute of Information Technology, PMAS-AAUR
Lecture 10: Programming Fundamentals:2012Lecture 01: Programming Fundamentals
Lecture 10
Sorting
1
University Institute of Information Technology, PMAS-AAUR
Lecture 10: Programming Fundamentals:2012
Sorting Arrays
• The process of arranging the values of an array
in particular order is called sorting.
• Two Types of sorting:
• Ascending Order: The smallest value is stored in the
first location of an array. The largest value is stored at
the last position of an array.
15 25 33 47 51
• Descending Order: The largest value is stored in the first
location of an array. The smallest value is stored at the
last position of an array.
51 47 33 25 15
2
University Institute of Information Technology, PMAS-AAUR
Lecture 10: Programming Fundamentals:2012
3
Given an array of length n,
– Search elements 0 through n-1 and select the smallest
• Swap it with the element in location 0
– Search elements 1 through n-1 and select the smallest
• Swap it with the element in location 1
– Search elements 2 through n-1 and select the smallest
• Swap it with the element in location 2
– Search elements 3 through n-1 and select the smallest
• Swap it with the element in location 3
– Continue in this fashion until there’s nothing left to
search
University Institute of Information Technology, PMAS-AAUR
Lecture 10: Programming Fundamentals:2012
4
Example and analysis of selection sort
• The selection sort might swap an
array element with itself--this is
harmless, and not worth checking
for
• Analysis:
– The outer loop executes n-1 times
– The inner loop executes about n/2
times on average (from n to 2
times)
– Work done in the inner loop is
constant (swap two array
elements)
– Time required is roughly (n-
1)*(n/2)
– You should recognize this as O(n2)
7 2 8 5 4
2 7 8 5 4
2 4 8 5 7
2 4 5 8 7
2 4 5 7 8
University Institute of Information Technology, PMAS-AAUR
Lecture 10: Programming Fundamentals:2012
Selection Sort
Program that take 5 random values from user
in an array and sort it in ascending order.
5
University Institute of Information Technology, PMAS-AAUR
Lecture 10: Programming Fundamentals:2012
int array[5], i, j, temp;
cout<< "Enter five integers" << endl;
for(i=0; i<5; i++)
{
cin>>array[i];
}
cout<<"The array in Actual Order n"<<endl;
for(i=0; i<5; i++)
{
cout<<array[i]<<" ";
}
for(i=0; i<5; i++)
for(j=i+1; j<5; j++)
if(array[i]>array[j])
{
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
cout<<endl;
cout<<"The array in Sorted Ordern ";
for(i=0; i<5; i++)
{
cout<<array[i]<<" ";
}
getch();
}
6
University Institute of Information Technology, PMAS-AAUR
Lecture 10: Programming Fundamentals:2012
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.
7
University Institute of Information Technology, PMAS-AAUR
Lecture 10: Programming Fundamentals:2012
• 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.
• So, 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).
• The bubble sort keeps track of occurring swaps by the use
of a flag.
8
University Institute of Information Technology, PMAS-AAUR
Lecture 10: Programming Fundamentals:2012
• The table below follows an array of numbers before,
during, and after a bubble sort for descending order.
• A "pass" is defined as one full trip through the array
comparing and if necessary, swapping, adjacent
elements.
• Several passes have to be made through the array
before it is finally sorted.
9
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
University Institute of Information Technology, PMAS-AAUR
Lecture 10: Programming Fundamentals:2012
Program that take 5 random values from user
in an array and sort it in ascending order,
using bubble sort.
10
University Institute of Information Technology, PMAS-AAUR
Lecture 10: Programming Fundamentals:2012
main()
{
int array[5], i, j, temp;
cout<< "Enter five integers" << endl;
for(i=0; i<5; i++)
{
cin>>array[i];
}
cout<<"The array in Actual Order n"<<endl;
for(i=0; i<5; i++)
{
cout<<array[i]<<" ";
}
for(i=0; i<5; i++)
for(j=0; j<4; j++)
if(array[j]>array[j+1])
{
temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
cout<<endl;
cout<<"The array in Sorted Ordern ";
for(i=0; i<5; i++)
{
cout<<array[i]<<" ";
}
getch();
} 11

Array sorting

  • 1.
    University Institute ofInformation Technology, PMAS-AAUR Lecture 10: Programming Fundamentals:2012Lecture 01: Programming Fundamentals Lecture 10 Sorting 1
  • 2.
    University Institute ofInformation Technology, PMAS-AAUR Lecture 10: Programming Fundamentals:2012 Sorting Arrays • The process of arranging the values of an array in particular order is called sorting. • Two Types of sorting: • Ascending Order: The smallest value is stored in the first location of an array. The largest value is stored at the last position of an array. 15 25 33 47 51 • Descending Order: The largest value is stored in the first location of an array. The smallest value is stored at the last position of an array. 51 47 33 25 15 2
  • 3.
    University Institute ofInformation Technology, PMAS-AAUR Lecture 10: Programming Fundamentals:2012 3 Given an array of length n, – Search elements 0 through n-1 and select the smallest • Swap it with the element in location 0 – Search elements 1 through n-1 and select the smallest • Swap it with the element in location 1 – Search elements 2 through n-1 and select the smallest • Swap it with the element in location 2 – Search elements 3 through n-1 and select the smallest • Swap it with the element in location 3 – Continue in this fashion until there’s nothing left to search
  • 4.
    University Institute ofInformation Technology, PMAS-AAUR Lecture 10: Programming Fundamentals:2012 4 Example and analysis of selection sort • The selection sort might swap an array element with itself--this is harmless, and not worth checking for • Analysis: – The outer loop executes n-1 times – The inner loop executes about n/2 times on average (from n to 2 times) – Work done in the inner loop is constant (swap two array elements) – Time required is roughly (n- 1)*(n/2) – You should recognize this as O(n2) 7 2 8 5 4 2 7 8 5 4 2 4 8 5 7 2 4 5 8 7 2 4 5 7 8
  • 5.
    University Institute ofInformation Technology, PMAS-AAUR Lecture 10: Programming Fundamentals:2012 Selection Sort Program that take 5 random values from user in an array and sort it in ascending order. 5
  • 6.
    University Institute ofInformation Technology, PMAS-AAUR Lecture 10: Programming Fundamentals:2012 int array[5], i, j, temp; cout<< "Enter five integers" << endl; for(i=0; i<5; i++) { cin>>array[i]; } cout<<"The array in Actual Order n"<<endl; for(i=0; i<5; i++) { cout<<array[i]<<" "; } for(i=0; i<5; i++) for(j=i+1; j<5; j++) if(array[i]>array[j]) { temp = array[i]; array[i] = array[j]; array[j] = temp; } cout<<endl; cout<<"The array in Sorted Ordern "; for(i=0; i<5; i++) { cout<<array[i]<<" "; } getch(); } 6
  • 7.
    University Institute ofInformation Technology, PMAS-AAUR Lecture 10: Programming Fundamentals:2012 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. 7
  • 8.
    University Institute ofInformation Technology, PMAS-AAUR Lecture 10: Programming Fundamentals:2012 • 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. • So, 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). • The bubble sort keeps track of occurring swaps by the use of a flag. 8
  • 9.
    University Institute ofInformation Technology, PMAS-AAUR Lecture 10: Programming Fundamentals:2012 • The table below follows an array of numbers before, during, and after a bubble sort for descending order. • A "pass" is defined as one full trip through the array comparing and if necessary, swapping, adjacent elements. • Several passes have to be made through the array before it is finally sorted. 9 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
  • 10.
    University Institute ofInformation Technology, PMAS-AAUR Lecture 10: Programming Fundamentals:2012 Program that take 5 random values from user in an array and sort it in ascending order, using bubble sort. 10
  • 11.
    University Institute ofInformation Technology, PMAS-AAUR Lecture 10: Programming Fundamentals:2012 main() { int array[5], i, j, temp; cout<< "Enter five integers" << endl; for(i=0; i<5; i++) { cin>>array[i]; } cout<<"The array in Actual Order n"<<endl; for(i=0; i<5; i++) { cout<<array[i]<<" "; } for(i=0; i<5; i++) for(j=0; j<4; j++) if(array[j]>array[j+1]) { temp = array[j]; array[j] = array[j+1]; array[j+1] = temp; } cout<<endl; cout<<"The array in Sorted Ordern "; for(i=0; i<5; i++) { cout<<array[i]<<" "; } getch(); } 11