0% found this document useful (0 votes)
58 views29 pages

Sorting: Spring Semester 2011 Programming and Data Structure 17

The document describes sorting algorithms including selection sort, insertion sort, and bubble sort. It provides examples to illustrate how each algorithm works by swapping elements in an array during successive passes until the array is fully sorted. Selection sort finds the minimum element in each pass and swaps it into place. Insertion sort inserts each element into the sorted portion of the array. Bubble sort compares and swaps adjacent elements, pushing larger values towards the end of the array over multiple passes.

Uploaded by

Pope Smith
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views29 pages

Sorting: Spring Semester 2011 Programming and Data Structure 17

The document describes sorting algorithms including selection sort, insertion sort, and bubble sort. It provides examples to illustrate how each algorithm works by swapping elements in an array during successive passes until the array is fully sorted. Selection sort finds the minimum element in each pass and swaps it into place. Insertion sort inserts each element into the sorted portion of the array. Bubble sort compares and swaps adjacent elements, pushing larger values towards the end of the array over multiple passes.

Uploaded by

Pope Smith
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

Sorting

Spring Semester 2011 Programming and Data Structure 17


The Basic Problem
• Given an array
x[0], x[1], ... , x[size-1]

reorder entries so that


x[0] <= x[1] <= . . . <= x[size-1]

• List is in non-decreasing order.

• We can also sort a list of elements in non-


increasing order.

Spring Semester 2011 Programming and Data Structure 18


Example

• Original list:
10, 30, 20, 80, 70, 10, 60, 40, 70

• Sorted in non-decreasing order:


10, 10, 20, 30, 40, 60, 70, 70, 80

• Sorted in non-increasing order:


80, 70, 70, 60, 40, 30, 20, 10, 10

Spring Semester 2011 Programming and Data Structure 19


Sorting Problem

• What we want ?
– Sort the given data sorted in the specified order
size-1
0
x: Unsorted list

Sorted list

Spring Semester 2011 Programming and Data Structure 20


Selection Sort

Spring Semester 2011 Programming and Data Structure 21


How it works?

• General situation :
0 k size-1
x: smallest elements, sorted remainder, unsorted

•Step :
•Find smallest element, mval, in x[k..size-1]
•Swap smallest element with x[k], then
increase k by one.
0 k mval size-1

swap
Spring Semester 2011 Programming and Data Structure 22
An example worked out

PASS 1:
10 5 17 11 -3 12 Find the minimum
10 5 17 11 -3 12 exchange with 0th
-3 5 17 11 10 12 element
PASS 2:
-3 5 17 11 10 12 Find the minumum
-3 5 17 11 10 12 exchange with 1st
-3 5 17 11 10 12 element
PASS 3:
-3 5 17 11 10 12 Find the minumum
-3 5 17 11 10 12 exchange with 2nd
-3 5 10 11 17 12 element

Spring Semester 2011 Programming and Data Structure 23


PASS 4:
-3 5 10 11 17 12 Find the minumum
-3 5 10 11 17 12 exchange with 3rd
-3 5 10 11 17 12 element
PASS 5:
-3 5 10 11 17 12 Find the minumum
-3 5 10 11 17 12 exchange with 4th
-3 5 10 11 12 17 element

Spring Semester 2011 Programming and Data Structure 24


Subproblem
/* Yield index of smallest element in x[k..size-1];*/

int min_loc (int x[], int k, int size)


{
int j, pos;

pos = k;
for (j=k+1; j<size; j++)
if (x[j] < x[pos])
pos = j;
return pos;
}

Spring Semester 2011 Programming and Data Structure 25


The main sorting function
/* Sort x[0..size-1] in non-decreasing order */

int sel_sort (int x[], int size)


{ int k, m;

for (k=0; k<size-1; k++)


{
m = min_loc (x, k, size);
temp = a[k];
a[k] = a[m]; SWAP
a[m] = temp;
}
}

Spring Semester 2011 Programming and Data Structure 26


int main()
{
int x[ ]={-45,89,-65,87,0,3,-23,19,56,21,76,-50};
int i;
for(i=0;i<12;i++)
printf("%d ",x[i]);
printf("\n");
-45 89 -65 87 0 3 -23 19 56 21 76 -50
sel_sort(x,12); -65 -50 -45 -23 0 3 19 21 56 76 87 89
for(i=0;i<12;i++)
printf("%d ",x[i]);
printf("\n");
}

Spring Semester 2011 Programming and Data Structure 27


Analysis

• How many steps are needed to sort n


items ?
– Total number of steps proportional to n2.
– No. of comparisons?
(n-1)+(n-2)+… … +1= n(n-1)/2

Of the order of n2

– Worst Case? Best Case? Average Case?

Spring Semester 2011 Programming and Data Structure 28


Insertion Sort

Spring Semester 2011 Programming and Data Structure 29


Basic Idea

• Insert elements one at a time, and create a partial


sorted list.
– Sorted list of 2 elements, 3 elements, 4 elements,
and so on.
• In general, in every iteration an element is
compared with all the elements before it.
• After finding the position of insertion, space is
created for it by shifting the other elements one
position up and the desired element is then inserted
at the suitable position.
• This procedure is repeated for all the elements in
the list.
Spring Semester 2011 Programming and Data Structure 30
An example worked out

PASS 1:
10 5 17 11 -3 12 item = 5
? 10 17 11 -3 12 compare 5 and 10
5 10 17 11 -3 12 insert 5
PASS 2:
5 10 17 11 -3 12 item = 17
5 10 17 11 -3 12 compare 17 and 10

PASS 3:
5 10 17 11 -3 12 item = 11
5 10 ? 17 -3 12 compare 11 and 17
5 10 ? 17 -3 12 compare 11 and 10
5 10 11 17 -3 12 insert 11

Spring Semester 2011 Programming and Data Structure 31


PASS 4:
5 10 11 17 -3 12 item = -3
5 10 11 ? 17 12 compare -3 and 17
5 10 ? 11 17 12 compare -3 and 11
5 ? 10 11 17 12 compare -3 and 10
? 5 10 11 17 12 compare -3 and 5
-3 5 10 11 17 12 insert -3
PASS 5:
-3 5 10 11 17 12 item = 12
-3 5 10 11 ? 17 compare 12 and 17
-3 5 10 11 ? 17 compare 12 and 11
-3 5 10 11 12 17 insert 12

Spring Semester 2011 Programming and Data Structure 32


Insertion Sort
void insert_sort (int list[], int size)
{
int i,j,item;

for (i=1; i<size; i++)


{
item = list[i] ;
j = i – 1;
while ((item < list[j]) && (j >= 0))
{
list[j+1] = list[j];
j--;
}
list[j+1] = item ;
}
}
Spring Semester 2011 Programming and Data Structure 33
int main()
{
int x[ ]={-45,89,-65,87,0,3,-23,19,56,21,76,-50};
int i;
for(i=0;i<12;i++)
printf("%d ",x[i]);
printf("\n");
-45 89 -65 87 0 3 -23 19 56 21 76 -50
insert_sort (x,12); -65 -50 -45 -23 0 3 19 21 56 76 87 89
for(i=0;i<12;i++)
printf("%d ",x[i]);
printf("\n");
}

Spring Semester 2011 Programming and Data Structure 34


Time Complexity

• Number of comparisons and shifting:


– Worst case?
1 + 2 + 3 + … … + (n-1) = n(n-1)/2

– Best case?
1 + 1 + … … to (n-1) terms = (n-1)

Spring Semester 2011 Programming and Data Structure 35


Bubble Sort

Spring Semester 2011 Programming and Data Structure 36


How it works?

• The sorting process proceeds in several


passes.
– In every pass we go on comparing neighboring
pairs, and swap them if out of order.
– In every pass, the largest of the elements under
considering will bubble to the top (i.e., the right).
10 5 17 11 -3 12
5 10 17 11 -3 12
5 10 17 11 -3 12
5 10 11 17 -3 12
5 10 11 -3 17 12
Largest
5 10 11 -3 12 17

Spring Semester 2011 Programming and Data Structure 37


An example worked out

PASS 1:
10 5 17 11 -3 12
5 10 17 11 -3 12
5 10 17 11 -3 12
5 10 11 17 -3 12
5 10 11 -3 17 12
5 10 11 -3 12 17
PASS 2:
5 10 11 -3 12 17
5 10 11 -3 12 17
5 10 11 -3 12 17
5 10 -3 11 12 17
5 10 -3 11 12 17

Spring Semester 2011 Programming and Data Structure 38


PASS 3:
5 10 -3 11 12 17
5 10 -3 11 12 17
5 -3 10 11 12 17
5 -3 10 11 12 17
PASS 4:
5 -3 10 11 12 17
-3 5 10 11 12 17
-3 5 10 11 12 17
PASS 5:
-3 5 10 11 12 17 Sorted
-3 5 10 11 12 17

Spring Semester 2011 Programming and Data Structure 39


• How the passes proceed?
– In pass 1, we consider index 0 to n-1.
– In pass 2, we consider index 0 to n-2.
– In pass 3, we consider index 0 to n-3.
– … …
– … …
– In pass n-1, we consider index 0 to 1.

Spring Semester 2011 Programming and Data Structure 40


Bubble Sort
void swap(int *x, int *y) void bubble_sort
{ (int x[], int n)
int tmp = *x; {
*x = *y; int i,j;
*y = tmp;
} for (i=n-1; i>0; i--)
for (j=0; j<i; j++)
if (x[j] > x[j+1])
swap(&x[j],&x[j+1]);
}

Spring Semester 2011 Programming and Data Structure 41


int main()
{
int x[ ]={-45,89,-65,87,0,3,-23,19,56,21,76,-50};
int i;
for(i=0;i<12;i++)
printf("%d ",x[i]);
printf("\n");
-45 89 -65 87 0 3 -23 19 56 21 76 -50
bubble_sort (x,12); -65 -50 -45 -23 0 3 19 21 56 76 87 89
for(i=0;i<12;i++)
printf("%d ",x[i]);
printf("\n");
}

Spring Semester 2011 Programming and Data Structure 42


Time Complexity

• Number of comparisons :
– Worst case?

1 + 2 + 3 + … … + (n-1) = n(n-1)/2

– Best case?
Same

Spring Semester 2011 Programming and Data Structure 43


• How do you make best case with (n-1)
comparisons only?
– By maintaining a variable flag, to check if
there has been any swaps in a given pass.
– If no swaps during a pass, the array is already
sorted.

Spring Semester 2011 Programming and Data Structure 44


void bubble_sort (int x[], int n)
{
int i, j, flag;

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


{
flag = 0;
for (j=0; j<i; j++)
if (x[j] > x[j+1])
{
swap(&x[j],&x[j+1]);
flag = 1;
}
if (flag == 0) return;
}

Spring Semester 2011 Programming and Data Structure 45

You might also like