Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

DS Programs

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

Sorting Techniques

1. Bubble sort/exchange sort


2. Insertion sort
3. Selection sort
4. Merge sort
5. Quick sort

Sathi Durga Devi, Dept of IT


Bubble Sort

Sathi Durga Devi, Dept of


IT
Bubble sort\exchange sort
• Suppose the list of numbers a[1],a[2],…,a[n] is in memory. The
bubble sort algorithm works as follows.
Step 1- compare a[1] and a[2] and arrange them in the desired
order, so that a[1]<a[2].
then compare a[2] and a[3] and arrange them so that a[2]<a[3].
Then compare a[3] and a[4] and arrange them so that a[3]<
a[4]. Continue until we compare a[n-1] with a[n] and arrange
them so that a[n-1]<a[n].
- Observe that step-1 involves n-1 comparisons. when step-1 is
completed a[n] has the largest element in the list. The
largest element is “bubbled up “ to the nth position.
Step 2- repeat step1 with one less comparision; it involves n-2
comparisions and when step2 is completed the second largest
element will occupy a[n-1].
Step 3- repeat step1 with two fewer comparisons; involves n-3
comparisons.
Step n-1. compare a[1] with a[2] and arrange them so that
a[1]<a[2].
After n-1 steps, the list will be sorted in increasing order.
The process of sequentially traversing through all or part of the
list is frequently called Sathi
a “pass”
Durga Devi, Dept of IT
Bubble sort
27 32 51 23 13

Pass-1 2 3 51 23 13
7 2

27 3 23 13
51
2

27 32 2 13
51
3
27 32 23 51 13

Sathi Durga Devi, Dept of IT


Bubble sort

Pass-2 2 3 23 13 51
7 2

27 3 2 13 51
2 3

27 23 3
13 51
2

27 23 13
32 51

Sathi Durga Devi, Dept of IT


Pass-3 27 23 13
32 51

2 2 13
7 3 32 51

23 2
13 32 51
7
23 13
27 32 51

Pass-4 2
13 27 32 51
3

Sorted Array after N-1 passes

13 23 27 32 51

Sathi Durga Devi, Dept of IT


/* Write a c program to implement the bubble sort */
#include<stdio.h>

void bubbleSort(int a[],int index,int size);


void main() {
int n,a[10],i;
printf("\n\nEnter the size of array: ");
scanf("%d",&n);
printf("\n\nEnter the elements: ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\n\nElements before sorting: ");
for(i=0;i<n;i++)
printf(" %d",a[i]);
printf("\n");
bubbleSort(a,0,n);
}

Sathi Durga Devi, Dept of IT


void bubblesort(int a[],int index, int n)
{
int i,x,j,temp;
for(i=1;i<=n-1;i++)
{
printf("\n pass- %d \n",i);
for(j=index;j<n-i;j++) Time complexity- O(n2)
{ if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}//if
for(x=0;x<n;x++)
printf("%3d",a[x]);
printf("\n\n");
}//j
}//i
printf("\n elements after sorting\n");
for(i=0;i<n;i++)
printf("%3d",a[i]); Sathi Durga Devi, Dept of IT
}//bublesort
Selection Sort
Procedure:
 Selection sort involved scanning through the list to select the smallest
element and swap it with the first element.
 The rest of the list is then search for the next smallest and swap it
with the second element.
 This process is repeated until the rest of the list reduces to one
element, by which time the list is sorted.
The following table shows how selection sort works.

Sathi Durga Devi, Dept of IT


9
77 33 44 55 88 66 22 11

Pass-1 77 33 44 55 88 66 22 11

Pass-2 11 33 44 55 88 66 22 77

Pass-3 11 22 44 55 88 66 33 77

Pass-4 11 22 33 55 88 66 44 77

Pass-5 11 22 33 44 88 66 55 77

Pass-6 11 22 33 44 55 66 88 77

Pass-7 11 22 33 44 55 66 88 77
Sathi Durga Devi, Dept of IT
Write a C program to implement Selection sort

#include<stdio.h>
void selectionsort(int a[],int n);
void main()
{
int a[100],i,n;
printf("\n enter the size of the array");
scanf("%d",&n);
printf("enter array elements");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
selectionsort(a,n);
}//main

Sathi Durga Devi, Dept of IT


for(j=i+1;j<=n-1;j++)
void selectionsort(int a[],int n) {
{ if(min>a[j])
int i,j,k,loc,min,temp; {
for(i=0;i<n-1;i++) min=a[j];
{ loc=j;}
printf("\n"); }//j
printf("\nstep-%d",i+1); temp=a[i];
min=a[i]; a[i]=a[loc];
loc=i; a[loc]=temp;
for(k=0;k<n;k++)
printf("%3d",a[k]);
}//i
printf("\n\n sorted array is ");
for(k=0;k<n;k++)
printf("%3d",a[k]);
}//selectionsort
Sathi Durga Devi, Dept of IT
Insertion Sort
• Idea: like sorting a hand of playing cards
– Start with an empty left hand and the cards facing down
on the table.
– Remove one card at a time from the table, and insert it
into the correct position in the left hand
• compare it with each of the cards already in the hand,
from right to left
– The cards held in the left hand are sorted
these cards were originally the top cards of the pile on
the table

Sathi Durga Devi, Dept of IT


Insertion Sort

To insert 12, we need to make


room for it by moving first 36
and then 24.
6 10 24 36

12

Sathi Durga Devi, Dept of IT


Insertion Sort

6 10 24 36

12

Sathi Durga Devi, Dept of IT


Insertion Sort

6 10 24 3
6

12

Sathi Durga Devi, Dept of IT


Insertion Sort
Using insertion sort an element is inserted in correct location.
Insertion sort scans the list from A[0] to A[n-1], insert each element A[k] into its
proper position in previously sorted sub list A[0],A[1],A[2]…A[k-1]..
Procedure:
 A[0] by itself is trivially sorted.
 A[1] is inserted either before or after A[0] so that A[0],A[1] is sorted.
 A[2] is inserted into its proper place in A[0],A[1], that is, before A[0], between
A[0] and A[1], or after A[1] so that A[0],A[1],A[2] is sorted.
 Repeatedly compare A[k] to the element just to the left of the vacancy, and as
long as A[k] is smaller, move that element into the vacancy, else put A[k] in
the vacancy.
 Repeat the next element that has not yet examined.
 Time complexity- O(n2)
 This algorithm is used when n is small.

Sathi Durga Devi, Dept of IT


17
Insertion Sort

Sathi Durga Devi, Dept of IT


18
Insertion Sort
input array
5 2 4 6 1 3

at each iteration, the array is divided in two sub-arrays:


left sub-array right sub-array

sorted unsorted

Sathi Durga Devi, Dept of IT


Insertion Sort

Sathi Durga Devi, Dept of IT


/*Write a program to implement the insertion sort*/
#include<stdio.h>
void insertsort(int a[],int n);
void main()
{
int i,a[10],n;
printf("\n enter array size");
scanf("%d",&n);
printf("\n enter array elements");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\n elements before swapping");
for(i=0;i<n;i++)
printf("%3d",a[i]);
insertsort(a,n);
printf("\n elements after swapping");
for(i=0;i<n;i++)
printf("%3d",a[i]);
}//main
Sathi Durga Devi, Dept of IT
void insertsort(int a[],int n)
{
int k,temp,j,i;
//steps
for(k=0;k<n;k++)
{
printf("\nstep-%d",k+1);
temp=a[k];
j=k-1;
while((j>=0)&&(temp<=a[j]))
{
a[j+1]=a[j];
a[j]=temp;
j--;
}//while
for(i=0;i<n;i++)
printf("%3d",a[i]);
}//for
}//insertsort
Sathi Durga Devi, Dept of IT
Merge Sort
 This algorithm uses the divide- and – conquer method.
 Split array A[0..n-1] into about equal halves and make copies of
each half in arrays B and C.
 Sort arrays B and C recursively.
Merge sorted arrays B and C into array A as follows:
 Repeat the following until no elements remain in one of the
arrays:
 Compare the first elements in the remaining unprocessed
portions of the arrays.
 Copy the smaller of the two into A, while incrementing the
index indicating the unprocessed portion of that array.
 Once all elements in one of the arrays are processed, copy the
remaining unprocessed elements from the other array into A.

Sathi Durga Devi, Dept of IT


Merge Sort

Sathi Durga Devi, Dept of IT


mergesort(a,0,7)
8 3 2 9 7 1 5 4

mergesort(a,0,3) mergesort(a,4,7)
8 3 2 9 7 1 5 4

mergesort(a,0,1) mergesort(a,2,3) mergesort(a,4,5) mergesort(a,6,7)


8 3 2 9 7 1 5 4

mergsort(a,0,0)
ms(a,1,1) ms(a,2,2) ms(a,3,3) ms(a,4,4) ms(a,5,5) ms(a,6,6) ms(a,7,7)
8 3 2 9 7 1 5 4

merge(a,0,1,0) merge(a,2,3,2) merge(a,4,5,4) merge(a,6,7,6)


3 8 2 9 1 7 4 5

merge(a,0,3,1) merge(a,4,7,5)
2 3 8 9 1 4 5 7

merge(a,0,7,3)
Sathi Durga Devi, Dept of IT
1 2 3 4 5 7 8 9
8 3 2 9 7 1 5 4

8 3 2 9 7 1 5 4

8 3 2 9 7 1 5 4

8 3 2 9 7 1 5 4

3 8 2 9 1 7 4 5

2 3 8 9 1 4 5 7

Sathi Durga Devi, Dept of IT


1 2 3 4 5 7 8 9
#include "stdio.h"
void merge(int [],int,int,int);
void mergesort(int a[],int low,int high)
Mergsort program
{
int mid;
if(low < high)
{
mid = (low + high)/2;
mergesort(a,low,mid);
mergesort(a,mid+1,high);
merge(a,low,high,mid);
} //if
}//mergesort

Sathi Durga Devi, Dept of IT


void merge(int a[],int l,int h,int m){
int c[100],i,j,k;
i = l; j = m + 1; k = l;
while (i <= m && j <= h)
{
if(a[i] < a[j])
{
c[k] = a[i]; i++; k++;
}//if
else
{
c[k] = a[j]; j++; k++;
} //else
}//while
while(i <= m) c[k++] = a[i++];
while(j <= h) c[k++] = a[j++];
for(i = l; i < k; i++) a[i] = c[i];
}//merge
Sathi Durga Devi, Dept of IT
void main()
{
int i,n,a[100];
printf("\n Enter the size of the array :");
scanf("%d",&n);
printf("\n Enter the elements :\n");
for(i = 0; i < n; i++)
scanf("%d",&a[i]);
mergesort(a,0,n-1);
printf("\n Elements in sorted order :\n");
for(i = 0; i < n; i++)
printf("%5d",a[i]);
getch();

Sathi Durga Devi, Dept of IT


Quick Sort

 This algorithm uses the divide- and – conquer method.


 Select a pivot element from the array of elements.
 Rearrange the list so that all the elements before the pivot are smaller than or
equal to the pivot and those after the pivot are larger than the pivot .
 After such a partitioning, the pivot is placed in its final position.
 Sort the two sublists recursively.

Sathi Durga Devi, Dept of IT


Quicksort Algorithm
Given an array of n elements (e.g., integers):
• If array only contains one element, return
• Else
– pick one element to use as pivot.
– Partition elements into two sub-arrays:
• Elements less than or equal to pivot
• Elements greater than pivot
– Quicksort two sub-arrays
– Return results

Sathi Durga Devi, Dept of IT


Sathi Durga Devi, Dept of IT
Recursive implementation with the left most array entry selected as
the pivot element.

Sathi Durga Devi, Dept of IT


/*Write a c program to implement the quick sort*/

#include<stdio.h>
void quicksort(int [10],int,int);

int main(){
int x[20],size,i;
printf("\nEnter size of the array: ");
scanf("%d",&size);
printf("\nEnter %d elements: ",size);
for(i=0;i<size;i++)
scanf("%d",&x[i]);
quicksort(x,0,size-1);
printf("\nSorted elements: ");
for(i=0;i<size;i++)
printf(" %d",x[i]);
getch();
return 0;
}
Sathi Durga Devi, Dept of IT
void quicksort(int x[10],int first,int last){
int pivot,j,temp,i;
if(first<last){
pivot=first;
i=first;
j=last;
while(i<j){
while(x[i]<=x[pivot]&&i<last)
i++;
while(x[j]>x[pivot])
j--;
if(i<j){
temp=x[i];
x[i]=x[j]; x[j]=temp;
}
}
temp=x[pivot];
x[pivot]=x[j];
x[j]=temp;
quicksort(x,first,j-1);
quicksort(x,j+1,last);
}
Sathi Durga Devi, Dept of IT
}
pivot_index = 0 40 20 10 80 60 50 7 30 100

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

i j

Sathi Durga Devi, Dept of IT


1. While x[i] <= x[pivot]
++i

pivot_index = 0 40 20 10 80 60 50 7 30 100

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

i j

Sathi Durga Devi, Dept of IT


1. While x[i] <= x[pivot]
++i

pivot_index = 0 40 20 10 80 60 50 7 30 100

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

i j

Sathi Durga Devi, Dept of IT


1. Whilex[i] <= x[pivot]
++i

pivot_index = 0 40 20 10 80 60 50 7 30 100

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

i j

Sathi Durga Devi, Dept of IT


1. While x[i] <= x[pivot]
++i
2. While x[j] > x[pivot]
--j

pivot_index = 0 40 20 10 80 60 50 7 30 100

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

i j

Sathi Durga Devi, Dept of IT


1. While x[i] <= x[pivot]
++i
2. While x[j] > x[pivot]
--j

pivot_index = 0 40 20 10 80 60 50 7 30 100

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

i j

Sathi Durga Devi, Dept of IT


1. While x[i] <= x[pivot]
++i
2. While x[j] > x[pivot]
--j
3. If i < j
swap x[i] and x[j]

pivot_index = 0 40 20 10 80 60 50 7 30 100

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

i j

Sathi Durga Devi, Dept of IT


1. While x[i] <= x[pivot]
++i
2. While x[j] > x[pivot]
--j
3. If i < j
swap x[i] and x[j]

pivot_index = 0 40 20 10 30 60 50 7 80 100

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

i j

Sathi Durga Devi, Dept of IT


1. While x[i] <= x[pivot]
++i
2. While x[j] > x[pivot]
--j
3. If i < j
swap x[i] and x[j]
4. While j > i, go to 1.

pivot_index = 0 40 20 10 30 60 50 7 80 100

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

i j

Sathi Durga Devi, Dept of IT


1. While x[i] <= x[pivot]
++i
2. While x[j] > x[pivot]
--j
3. If i < j
swap x[i] and x[j]
4. While j > i, go to 1.

pivot_index = 0 40 20 10 30 60 50 7 80 100

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

i j

Sathi Durga Devi, Dept of IT


1. While x[i] <= x[pivot]
++i
2. While x[j] > x[pivot]
--j
3. If i < j
swap x[i] and x[j]
4. While j > i, go to 1.

pivot_index = 0 40 20 10 30 60 50 7 80 100

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

i j

Sathi Durga Devi, Dept of IT


1. While x[i] <= x[pivot]
++i
2. While x[j] > x[pivot]
--j
3. If i < j
swap x[i] and x[j]
4. While j > i, go to 1.

pivot_index = 0 40 20 10 30 60 50 7 80 100

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

i j

Sathi Durga Devi, Dept of IT


1. While data[i] <= data[pivot]
++i
2. While data[j] > data[pivot]
--j
3. If i < j
swap data[i] and data[j]
4. While j > i, go to 1.

pivot_index = 0 40 20 10 30 60 50 7 80 100

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

i j

Sathi Durga Devi, Dept of IT


1. While x[i] <= x[pivot]
++i
2. While x[j] >x[pivot]
--j
3. If i < j
swap x[i] and x[j]
4. While j > i, go to 1.

pivot_index = 0 40 20 10 30 60 50 7 80 100

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

i j

Sathi Durga Devi, Dept of IT


1. While x[i] <= x[pivot]
++i
2. While x[j] > x[pivot]
--j
3. If i < j
swap x[i] and x[j]
4. While j > i, go to 1.

pivot_index = 0 40 20 10 30 7 50 60 80 100

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

i j

Sathi Durga Devi, Dept of IT


1. While x[i] <= x[pivot]
++i
2. While x[j] > x[pivot]
--j
3. If i < j
swap x[i] and x[j]
4. While j > i, go to 1.

pivot_index = 0 40 20 10 30 7 50 60 80 100

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

i j

Sathi Durga Devi, Dept of IT


1. While x[i] <= x[pivot]
++i
2. While x[j] > x[pivot]
--j
3. If i < j
swap x[i] and x[j]
4. While j > i, go to 1.

pivot_index = 0 40 20 10 30 7 50 60 80 100

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

i j

Sathi Durga Devi, Dept of IT


1. While x[i] <= x[pivot]
++i
2. While x[j] > x[pivot]
--j
3. If i < j
swap x[i] and x[j]
4. While j > i, go to 1.

pivot_index = 0 40 20 10 30 7 50 60 80 100

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

i j

Sathi Durga Devi, Dept of IT


1. While data[i] <= data[pivot]
++i
2. While data[j] > data[pivot]
--j
3. If i < j
swap data[i] and data[j]
4. While j > i, go to 1.

pivot_index = 0 40 20 10 30 7 50 60 80 100

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

i j

Sathi Durga Devi, Dept of IT


1. While data[i] <= data[pivot]
++i
2. While data[j] > data[pivot]
--j
3. If i < j
swap data[i] and data[j]
4. While j > i, go to 1.

pivot_index = 0 40 20 10 30 7 50 60 80 100

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

i j

Sathi Durga Devi, Dept of IT


1. While x[i] <= x[pivot]
++i
2. While x[j] > x[pivot]
--j
3. If i < j
swap x[i] and x[j]
4. While j > i, go to 1.

pivot_index = 0 40 20 10 30 7 50 60 80 100

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

i j

Sathi Durga Devi, Dept of IT


1. While x[i] <= x[pivot]
++i
2. While x[j] > x[pivot]
--j
3. If i < j
swap x[i] and x[j]
4. While j > i, go to 1.

pivot_index = 0 40 20 10 30 7 50 60 80 100

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

i j

Sathi Durga Devi, Dept of IT


1. While x[i] <= x[pivot]
++i
2. While x[j] > x[pivot]
--j
3. If i < j
swap x[i] and x[j]
4. While j > i, go to 1.

pivot_index = 0 40 20 10 30 7 50 60 80 100

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

i j

Sathi Durga Devi, Dept of IT


1. While x[i] <= x[pivot]
++i
2. While x[j] > x[pivot]
--j
3. If i < j
swap x[i] and x[j]
4. While j > i, go to 1.
5. Swap x[j] and x[pivot_index]

pivot_index = 0 40 20 10 30 7 50 60 80 100

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

i j

Sathi Durga Devi, Dept of IT


1. While x[i] <= x[pivot]
++i
2. While x[j] > x[pivot]
--j
3. If i < j
swap x[i] and x[j]
4. While j > i, go to 1.
5. Swap x[j] and x[pivot_index]

pivot_index = 4 7 20 10 30 40 50 60 80 100

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

i j

Sathi Durga Devi, Dept of IT


Partition Result

7 20 10 30 40 50 60 80 100

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

<= data[pivot] > data[pivot]

Sathi Durga Devi, Dept of IT


Recursion: Quicksort Sub-arrays

7 20 10 30 40 50 60 80 100

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

<= data[pivot] > data[pivot]

Sathi Durga Devi, Dept of IT


• Example
44,33,11,55,77,90,40,60,99,22,66

Sathi Durga Devi, Dept of IT


 Big O Notation
 Indicates the worst-case run time for an algorithm.
 In other words, how hard an algorithm has to work to solve a problem.
Time Complexities of Searching & Sorting Algorithms:

Average Worst Case


Case
Linear Search - O(n)
Binary Search O(log n) O(n)
Bubble Sort O(n2) O(n2)
Selection Sort - O(n2)
Insertion Sort - O(n2)
Merge Sort O(n log n) O(n log n)
Quick Sort O(n log n) O(n2)

Sathi Durga Devi, Dept of IT


Review of Algorithms
• Selection Sort
– An algorithm which orders items by repeatedly looking through remaining
items to find the least one and moving it to a final location
• Bubble Sort
– Sort by comparing each adjacent pair of items in a list in turn, swapping the
items if necessary, and repeating the pass through the list until no swaps are
done
• Insertion Sort
– Sort by repeatedly taking the next item and inserting it into the final data
structure in its proper order with respect to items already inserted.
• Merge Sort
– An algorithm which splits the items to be sorted into two groups, recursively
sorts each group, and merges them into a final, sorted sequence
• Quick Sort
– An in-place sort algorithm that uses the divide and conquer paradigm. It picks
an element from the array (the pivot), partitions the remaining elements into
those greater than and less than this pivot, and recursively sorts the partitions.

Sathi Durga Devi, Dept of IT

You might also like