Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 59

Sorting Arrays

WEEK # 13
LECTURE 2

Sorting Arrays
Sorting is the process of putting data in order; either numerically or alphabetically. It is often necessary to arrange the elements in an array in numerical order from highest to lowest values (descending order) or vice versa (ascending order). If the array contains string values, alphabetical order may be needed (which is actually ascending order using ASCII values). Searching for an element in an array will be more efficient. (example: looking for a particular phone number).

Sorting Arrays
The process of sorting an array requires the exchanging of values. While this seems to be a simple process, we must be careful that no values are lost during this exchange. Suppose that num[1] = 10 and num[2] = 8 and you want to exchange their values so that num[1] = 8 and num[2] = 10. could NOT just do this:

num[1] = num[2]; num[2] = num[1];

Sorting Arrays
In the first step, the value stored in num[1] is erased and replaced with num[2]. The result is that both num[1] and num[2] have the same value. Value in num[1] is lost!!! To swap two values, you must use a third variable, to hold the value you do not want to lose:

temp = num[1]; num[1] = num[2]; num[2] = temp;

Sorting Algorithms
There are different ways to sort arrays. Every methods is the same: to compare each array element to another array element and swap them if they are in the wrong position.

Bubble Sort Selection Sort Insertion Sort Bucket Sort

Selection Sort
Find the smallest element in the array and exchange it with the element in the first position. Then, find the second smallest element and exchange it with the element in the second position, and so on until the entire array is sorted. The Algorithm: Input: An array A storing N items Output: A sorted in ascending order

Selection Sort
Example: we are given an array of six integers that we want to sort from smallest to largest

70 60 50 40 30 20 10 0 [1] [0] [2] [1] [2][3] [3] [4] [4] [5] [5] [6]

Selection Sort
Start by finding the smallest entry.

70 60 50 40 30 20 10 0 [1] [0] [2] [1]

[2][3] [3] [4] [4]

[5] [5]

[6]

Selection Sort
Swap the smallest entry with the first entry.

70 60 50 40 30 20 10 0 [1] [0] [2] [1]

[2][3] [3] [4] [4]

[5] [5]

[6]

Selection Sort
Swap the smallest entry with the first entry.

70 60 50 40 30 20 10 0 [1] [0] [2] [1]

[2][3] [3] [4] [4]

[5] [5]

[6]

Selection Sort
Sorted side
70

Unsorted side

Part of the array is now sorted.

60 50 40 30 20 10 0 [1] [0] [2] [1]

[2][3] [3] [4] [4]

[5] [5]

[6]

Selection Sort
Sorted side
70

Unsorted side

Find the smallest element in the unsorted side.

60 50 40 30 20 10 0 [1] [0] [2] [1]

[2][3] [3] [4] [4]

[5] [5]

[6]

Selection Sort
Sorted side
70

Unsorted side

Swap with the front of the unsorted side.

60 50 40 30 20 10 0 [1] [0] [2] [1]

[2][3] [3] [4] [4]

[5] [5]

[6]

Selection Sort
Sorted side
70

Unsorted side

We have increased the size of the sorted side by one element.

60 50 40 30 20 10 0 [1] [0] [2] [1]

[2][3] [3] [4] [4]

[5] [5]

[6]

Selection Sort
Sorted side
70

Unsorted side

The process continues...

60 50 40 30 20 10 0 [1] [0] [2] [1]

Smallest from unsorted

[2][3] [3] [4] [4]

[5] [5]

[6]

Selection Sort
Sorted side
70

Unsorted side

The process continues...

60 50 40 30 20 10 0 [1] [0] [2] [1]

[2][3] [3] [4] [4]

[5] [5]

[6]

Selection Sort
Sorted side is bigger
70

Sorted side

Unsorted side

The process continues...

60 50 40 30 20 10 0 [1] [0] [2] [1]

[2][3] [3] [4] [4]

[5] [5]

[6]

Selection Sort
The process keeps adding one more number to the sorted side. The sorted side has the smallest numbers, arranged from small to large.
Sorted side
70 60 50 40 30 20 10 0 [1] [0] [2] [1]

Unsorted side

[2][3] [3] [4] [4]

[5] [5]

[6]

Selection Sort
We can stop when the unsorted side has just one number, since that number must be the largest number.
Sorted side
70 60 50 40 30 20 10 0 [1] [0] [2] [1]

Unsorted side

[2][3] [3] [4] [4]

[5] [5]

[6]

Selection Sort
The array is now sorted. We repeatedly selected the smallest element, and moved this element to the front of the unsorted side.

70 60 50 40 30 20 10 0 [1] [0] [2] [1]

[2][3] [3] [4] [4]

[5] [5]

[6]

Selection Sort

a[5]={78,23,90,5,89};

Selection Sort
include<iostream.h> void main() { int a[5]={78,23,90,5,89}; int i=0,j=0; int temp; cout<<"Values in array : "; for (i=0;i<5;i++) { cout<<a[i]<<" "; } cout<<endl<<endl; for (i=0;i<4;i++) { for(j=i+1;j<5;j++) { if (a[j]<a[i]) { temp=a[i]; a[i]=a[j]; a[j]=temp; } } }

cout<<"Values in array : "; for (i=0;i<5;i++) { cout<<a[i]<<" "; } cout<<endl<<endl; }

Selection Sort

a[10]={78,23,90,5,89,4,78,92,56,63};

Selection Sort
include<iostream.h> void main(){ int a[10]={78,23,90,5,89,4,78,92,56,63}; int i=0,j=0; int temp; cout<<"Values in array : "; for (i=0;i<10;i++) { cout<<a[i]<<" "; } cout<<endl<<endl; for (i=0;i<9;i++) { for(j=i+1;j<10;j++) { if (a[j]<a[i]) { temp=a[i]; a[i]=a[j]; a[j]=temp; } } }

cout<<"Values in array : "; for (i=0;i<10;i++) { cout<<a[i]<<" "; } cout<<endl<<endl; }

General Program
#include<iostream.h> void main() { int a[100]; int i=0,j=0; int size; int temp; cout<<"Enter size of array : "; cin>>size; cout<<"Enter Values in array : "<<endl; for (i=0;i<size;i++) { cin>>a[i]; } cout<<endl<<"Values in array : "; for (i=0;i<size;i++) { cout<<a[i]<<" "; } cout<<endl<<endl; for (i=0;i<size-1;i++) { for(j=i+1;j<size;j++) { if (a[j]<a[i]) { temp=a[i]; a[i]=a[j]; a[j]=temp; } } } cout<<"Values in array : "; for (i=0;i<size;i++) { cout<<a[i]<<" "; } cout<<endl<<endl; }

Selection Sort (Function)


#include<iostream.h> void sort (int a[], int size); void main() { int a[100]; int i=0,j=0; int size; cout<<"Enter size of array : "; cin>>size; cout<<"Enter Values in array : "<<endl; for (i=0;i<size;i++) { cin>>a[i]; } cout<<endl<<"Values in array : "; for (i=0;i<size;i++) { cout<<a[i]<<" "; } cout<<endl<<endl; sort(a,size); cout<<"Values in array : "; for (i=0;i<size;i++) { cout<<a[i]<<" "; } cout<<endl<<endl; }

void sort (int a[], int size) {int temp; for (int i=0;i<size-1;i++) { for(int j=i+1;j<size;j++) { if (a[j]<a[i]) { temp=a[i]; a[i]=a[j]; a[j]=temp; } } } }

#include<iostream.h> void sort (int a[], int size); void output (int a[],int size); void input (int a[],int size); void main() { int a[100],b[50]; int size;

cout<<"Enter size of A array : "; cin>>size; input(a,size); output(a,size); sort(a,size); output(a,size); cout<<endl<<endl; cout<<"Enter size of B array : "; cin>>size; input(b,size); output(b,size); sort(b,size); output(b,size); cout<<endl<<endl; }

void input (int a[],int size) { cout<<"Enter Values in array : "<<endl; for (int i=0;i<size;i++) { cin>>a[i]; } } void output (int a[],int size) { cout<<endl<<"Values in array : "; for (int i=0;i<size;i++) { cout<<a[i]<<" "; } } void sort (int a[], int size) {int temp; for (int i=0;i<size-1;i++) { for(int j=i+1;j<size;j++) { if (a[j]<a[i]) { temp=a[i]; a[i]=a[j]; a[j]=temp; } }}}

Selection Sort
for i:= 0 to LAST -1 { for j:= i+1 to LAST do { if A[j] < A[i] then{ temp := A[i], A[i] := A[j], A[j] := temp}} }

Selection Sort
Find the largest element in the array and exchange it with the element in the first position. Then, find the second largest element and exchange it with the element in the second position, and so on until the entire array is sorted.

Selection Sort
include<iostream.h> void main() { int a[5]={78,23,90,5,89}; int i=0,j=0; int temp; cout<<"Values in array : "; for (i=0;i<5;i++) { cout<<a[i]<<" "; } cout<<endl<<endl; for (i=0;i<4;i++) { for(j=i+1;j<5;j++) { if (a[j]>a[i]) { temp=a[i]; a[i]=a[j]; a[j]=temp; } } }

cout<<"Values in array : "; for (i=0;i<5;i++) { cout<<a[i]<<" "; } cout<<endl<<endl; }

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.

Bubble Sort
Make repeated passes through a list of items, exchanging adjacent items if necessary. At each pass, the largest unsorted item will be pushed in its proper place. Stop when no more exchanges were performed during the last pass. The Algorithm: Input: An array A storing N items Output: A sorted in ascending order

Bubble Sort
The Bubble Sort algorithm looks at pairs of entries in the array, and swaps their order if needed.

70 60 50 40 30 20 10 0 [1] [0] [2] [1]

[2][3] [3] [4] [4]

[5] [5]

[6]

Bubble Sort
The Bubble Sort algorithm looks at pairs of entries in the array, and swaps their order if needed.
Swap?

70 60 50 40 30 20 10 0 [1] [0] [2] [1]

[2][3] [3] [4] [4]

[5] [5]

[6]

Bubble Sort
The Bubble Sort algorithm looks at pairs of entries in the array, and swaps their order if needed.
Yes!

70 60 50 40 30 20 10 0 [1] [0] [2] [1]

[2][3] [3] [4] [4]

[5] [5]

[6]

Bubble Sort
The Bubble Sort algorithm looks at pairs of entries in the array, and swaps their order if needed.
Swap?

70 60 50 40 30 20 10 0 [1] [0] [2] [1]

[2][3] [3] [4] [4]

[5] [5]

[6]

Bubble Sort
The Bubble Sort algorithm looks at pairs of entries in the array, and swaps their order if needed.
No.

70 60 50 40 30 20 10 0 [1] [0] [2] [1]

[2][3] [3] [4] [4]

[5] [5]

[6]

Bubble Sort
The Bubble Sort algorithm looks at pairs of entries in the array, and swaps their order if needed.
Swap?

70 60 50 40 30 20 10 0 [1] [0] [2] [1]

[2][3] [3] [4] [4]

[5] [5]

[6]

Bubble Sort
The Bubble Sort algorithm looks at pairs of entries in the array, and swaps their order if needed.
No.

70 60 50 40 30 20 10 0 [1] [0] [2] [1]

[2][3] [3] [4] [4]

[5] [5]

[6]

Bubble Sort
The Bubble Sort algorithm looks at pairs of entries in the array, and swaps their order if needed.
Swap?

70 60 50 40 30 20 10 0 [1] [0] [2] [1]

[2][3] [3] [4] [4]

[5] [5]

[6]

Bubble Sort
The Bubble Sort algorithm looks at pairs of entries in the array, and swaps their order if needed.
Yes!

70 60 50 40 30 20 10 0 [1] [0] [2] [1]

[2][3] [3] [4] [4]

[5] [5]

[6]

Bubble Sort
The Bubble Sort algorithm looks at pairs of entries in the array, and swaps their order if needed.
Swap?

70 60 50 40 30 20 10 0 [1] [0] [2] [1]

[2][3] [3] [4] [4]

[5] [5]

[6]

Bubble Sort
The Bubble Sort algorithm looks at pairs of entries in the array, and swaps their order if needed.
Yes!

70 60 50 40 30 20 10 0 [1] [0] [2] [1]

[2][3] [3] [4] [4]

[5] [5]

[6]

Bubble Sort
Repeat.
70 60 50 40 30 20 10

Swap? No.

0 [1] [0] [2] [1]

[2][3] [3] [4] [4]

[5] [5]

[6]

Bubble Sort
Repeat.
70 60 50 40 30 20 10

Swap? No.

0 [1] [0] [2] [1]

[2][3] [3] [4] [4]

[5] [5]

[6]

Bubble Sort
Repeat.
70 60 50 40 30 20 10

Swap? Yes.

0 [1] [0] [2] [1]

[2][3] [3] [4] [4]

[5] [5]

[6]

Bubble Sort
Repeat.
70 60 50 40 30 20 10

Swap? Yes.

0 [1] [0] [2] [1]

[2][3] [3] [4] [4]

[5] [5]

[6]

Bubble Sort
Repeat.
70 60 50 40 30 20 10

Swap? Yes.

0 [1] [0] [2] [1]

[2][3] [3] [4] [4]

[5] [5]

[6]

Bubble Sort
Repeat.
70 60 50 40 30 20 10

Swap? Yes.

0 [1] [0] [2] [1]

[2][3] [3] [4] [4]

[5] [5]

[6]

Bubble Sort
Loop over array n-1 times, swapping pairs of entries as needed.

70 60 50 40 30 20 10

Swap? No.

0 [1] [0] [2] [2] [3] [3] [4] [1] [4] [5] [5] [6]

Bubble Sort
Loop over array n-1 times, swapping pairs of entries as needed.

70 60 50 40 30 20 10

Swap? Yes.

0 [1] [0] [2] [2] [3] [3] [4] [1] [4] [5] [5] [6]

Bubble Sort
Loop over array n-1 times, swapping pairs of entries as needed.

70 60 50 40 30 20 10

Swap? Yes.

0 [1] [0] [2] [2] [3] [3] [4] [1] [4] [5] [5] [6]

Bubble Sort
Loop over array n-1 times, swapping pairs of entries as needed.

70 60 50 40 30 20 10

Swap? Yes.

0 [1] [0] [2] [2] [3] [3] [4] [1] [4] [5] [5] [6]

Bubble Sort
Loop over array n-1 times, swapping pairs of entries as needed.

70 60 50 40 30 20 10

Swap? Yes.

0 [1] [0] [2] [2] [3] [3] [4] [1] [4] [5] [5] [6]

Bubble Sort
Continue looping, until done.

70 60 50 40 30 20 10

Swap? Yes.

0 [1] [0] [2] [2] [3] [3] [4] [1] [4] [5] [5] [6]

Bubble Sort
for i := 0 to LAST-1 { for j := 0 to LAST-i { if A[ j ] > A[ j+1 ] { temp := A[j], A[j] := A[j+1], A[j+1] := temp} } }

Bubble Sort
include<iostream.h> void main() { int a[5]={78,23,90,5,89}; int i=0,j=0; int temp; cout<<"Values in array : "; for (i=0;i<5;i++) { cout<<a[i]<<" "; } cout<<endl<<endl; { for (i=0;i<4;i++) for(j=0;j<4-i;j++) { if (a[j]>a[j+1]) { temp=a[j+1]; a[j+1]=a[j]; a[j]=temp; } } }

cout<<"Values in array : "; for (i=0;i<5;i++) { cout<<a[i]<<" "; } cout<<endl<<endl; }

General Program
#include<iostream.h> void main() { int a[100]; int i=0,j=0; int size; int temp; cout<<"Enter size of array : "; cin>>size; cout<<"Enter Values in array : "<<endl; for (i=0;i<size;i++) { cin>>a[i]; } cout<<endl<<"Values in array : "; for (i=0;i<size;i++) { cout<<a[i]<<" "; } cout<<endl<<endl; for (i=0;i<size-1;i++) { for(j=0;j<size-i;j++) { if (a[j]>a[j+1]) { temp=a[j+1]; a[j+1]=a[j]; a[j]=temp; } } } cout<<"Values in array : "; for (i=0;i<size;i++) { cout<<a[i]<<" "; } cout<<endl<<endl; }

Bubble Sort Selection Sort

You might also like