You are on page 1of 29

# GROUP MEMBERS

:
N. RajAsekarAN P.V.RajAselvam Rohit MAKHEJA D. Ravi Kumar roshni sasikala 10E631 10E632 10E634 10E633 10e635 11e912

Write a program to demonstrate bubble sort and quick sort. Your program should read a set of data given by the user and sort it in ascending order using the sorting algorithm selected by the user. Also count and display the number of data comparisons done by the two algorithms for a given input.

 A C++ program to demonstrate bubble sort and quick sort. It should read a set of data given by the user and sort it in ascending order using the sorting algorithm selected by the user. Count and display the number of data comparisons done by the two algorithms for a given input.   .

 Card players all know how to sort … ◦ First card is already sorted ◦ With all the rest. Move all the lower ones up one slot  insert it   A  K  10  2  J  2   9  2  Q  9  .  Scan back from the end until you find the first card larger than the new one.

 From the first element ◦ Exchange pairs if they’re out of order  Last one must now be the largest ◦ Repeat from the first to n-1 ◦ Stop when you have only one element to check .

/* Bubble sort for integers */ #define SWAP(a. t=a.a[j]).i<n.b) { int t. for(i=0. } } } .i++) { /* n passes thru the array */ /* From start to the end of unsorted part */ for(j=1. j. b=t. int n ) { int i.j++) { /* If adjacent items out of order. swap */ if( a[j-1]>a[j] ) SWAP(a[j-1]. } void bubble( int a[]. a=b.j<(n-i).

int n ) { int i. } } } O(1) statement . } void bubble( int a[]. a=b. for(i=0.b) { int t.i<n.j<(n-i).j++) { /* If adjacent items out of order. b=t. swap */ if( a[j-1]>a[j] ) SWAP(a[j-1]. t=a. j.a[j]).i++) { /* n passes thru the array */ /* From start to the end of unsorted part */ for(j=1./* Bubble sort for integers */ #define SWAP(a.

n-2./* Bubble sort for integers */ #define SWAP(a. int n ) { int i.i<n. a=b. } } Inner loop O(1) statement } n-1. swap */ if( a[j-1]>a[j] ) SWAP(a[j-1]. … . j.i++) { /* n passes thru the array */ /* From start to the end of unsorted part */ for(j=1. t=a.j++) { /* If adjacent items out of order. 1 iterations . } void bubble( int a[].j<(n-i).b) { int t. for(i=0. b=t. n-3.a[j]).

for(i=0. } } } Outer loop n iterations . t=a.b) { int t. int n ) { int i. b=t.i<n. } void bubble( int a[].j++) { /* If adjacent items out of order./* Bubble sort for integers */ #define SWAP(a.i++) { /* n passes thru the array */ /* From start to the end of unsorted part */ for(j=1.a[j]). swap */ if( a[j-1]>a[j] ) SWAP(a[j-1]. j.j<(n-i). a=b.

b=t. } } inner loop iteration count } n outer loop iterations .a[j])./* Bubble sort for integers */ #define SWAP(a.j<(n-i). j.i++) { /* n passes thru the array */ /* From start to the end of unsorted part */ 1 n(n+1) for(j=1. int n ) { int i. swap */ if( a[j-1]>a[j] ) SWAP(a[j-1]. } void bubble( int a[].i<n. t=a. a=b.b) { int t. Overall for(i=0.j++) { S i = = O(n2) i=n-1 /* If adjacent items2 out of order.

A. Hoare   Example of Divide and Conquer algorithm Two phases ◦ Partition phase  Divides the work into half ◦ Sort phase  Conquers the halves! . Efficient sorting algorithm ◦ Discovered by C.R.

 Partition ◦ Choose a pivot ◦ Find the position for the pivot so that  all elements to the left are less  all elements to the right are greater < pivot pivot > pivot .

 Conquer ◦ Apply the same algorithm to each half < pivot < p’ p’ > p’ pivot < p” > pivot p” > p” .

low. int low. Implementation quicksort( void *a. high ). high ). Conquer } } . Divide quicksort( a. /* Termination condition! */ if ( high > low ) { pivot = partition( a. quicksort( a. pivot+1. pivot-1 ). int high ) { int pivot. low.

right = high. if ( left < right ) SWAP(a. pivot = left = low. while ( left < right ) { /* Move left while item < pivot */ while( a[left] <= pivot_item ) left++. int high ) { int left. right. pivot_item = a[low]. return right. } . a[right] = pivot_item.left. } /* right is final position for the pivot */ a[low] = a[right].right).int partition( int *a. int pivot_item. int low. /* Move right while item > pivot */ while( a[right] >= pivot_item ) right--.

low high } This example uses int’s to keep things simple! . a[right] = pivot_item. if ( left < right ) SWAP(a. pivot_item = a[low]. Any item will do as the pivot. } 23 12 15 38 42 18 36 29 27 /* right is final position for the pivot */ a[low] = a[right]. return right. while ( left < right ) { choose the leftmost one! /* Move left while item < pivot */ while( a[left] <= pivot_item ) left++. pivot = left = low.right). right = high. /* Move right while item > pivot */ while( a[right] >= pivot_item ) right--.left. int pivot_item.int partition( int *a. int low. int high ) { int left. right.

23 12 < right 15 38 42 18 36 29 27 } /* right is final position for the pivot */ a[low] = a[right]. int low.int partition( int *a. Set left and right markers right = high. low high pivot: 23 a[right] = pivot_item. } . if ( left ) SWAP(a. right. pivot_item = a[low]. int pivot_item. pivot = left = low.right). return right. int high ) { int left.right /* Move right while item > pivot */ while( a[right] >= pivot_item ) right--. while ( left < right ) { /* Move left while item < pivot */ while( left a[left] <= pivot_item ) left++.left.

if ( left < right ) SWAP(a. int low. return right. right = high. int pivot_item.right). low pivot: 23 } right 27 high . left } /* right is final position for the pivot */ a[low] = a[right]. pivot_item = a[low]. pivot = left = low. 23 12 15 38 42 18 36 29 a[right] = pivot_item.left. /* Move right while item > pivot */ while( a[right] >= pivot_item ) right--.int partition( int *a. right. int high ) { int left. Move the markers until they cross over while ( left < right ) { /* Move left while item < pivot */ while( a[left] <= pivot_item ) left++.

/* Move right while item > pivot */ while( a[right] >= pivot_item ) right--. if ( left < right ) SWAP(a. left right } Move right /* right is final position for the pivot */ similarly a[low] = a[right]. Move the left pointer while it points to items <= pivot while ( left < right ) { /* Move left while item < pivot */ while( a[left] <= pivot_item ) left++. pivot_item = a[low]. int high ) { int left. } low high pivot: 23 . 23 12 15 38 42 18 36 29 27 a[right] = pivot_item. right = high. return right. int low. pivot = left = low.int partition( int *a. int pivot_item. right.left.right).

right. Swap the two items on the wrong side of the pivot while ( left < right ) { /* Move left while item < pivot */ while( a[left] <= pivot_item ) left++.int partition( int *a. pivot = left = low. int high ) { int left. /* Move right while item > pivot */ while( a[right] >= pivot_item ) right--. } left right /* right is final position for the pivot */ a[low] = a[right].right). 23 12 15 38 42 18 36 29 27 return right. if ( left < right ) SWAP(a. right = high.left. a[right] = pivot_item. pivot_item = a[low]. pivot: 23 } low high . int pivot_item. int low.

if ( left < right ) SWAP(a. 23 12 15 18 42 38 36 29 27 return right. /* Move right while item > pivot */ while( a[right] >= pivot_item ) right--. } right left /* right is final position for the pivot */ a[low] = a[right].right). right = high. a[right] = pivot_item. int high ) { int left.int partition( int *a. right.left. pivot_item = a[low]. so stop while ( left < right ) { /* Move left while item < pivot */ while( a[left] <= pivot_item ) left++. pivot = left = low. int low. } low high pivot: 23 . left and right have swapped over. int pivot_item.

Finally. right = high. pivot_item = a[low]. int pivot_item. } /* right is final position for the pivot */ a[low] = a[right]. int low. int high ) { int left. /* Move right while item > pivot */ while( a[right] >= pivot_item ) right--. if ( left < :right low high pivot 23 ) SWAP(a. pivot = left = low. } .left. and right return right. right. right left while ( left < right ) { /* Move left while item < pivot */ 23 12while( 15 a[left] 18 42 <= 38 pivot_item 36 29 ) 27left++.right).int partition( int *a. swap the pivot a[right] = pivot_item.

} . of the pivot return right. int low. int high ) { int left. right.int partition( int *a. pivot_item = a[low]. int pivot_item. if ( left < right ) SWAP(a. low high } /* right is final position for the pivot */ a[low] = a[right].left. Return the position a[right] = pivot_item. pivot = left = low. right while ( left < right ) { 18 pivot: 23 12while( 15 a[left] 23 42 <= 38 pivot_item 36 29 ) 27left++. right = high. /* Move left while item < pivot */ /* Move right while item > pivot */ while( a[right] >= pivot_item ) right--.right).

pivot pivot: 23 18 12 15 23 42 38 36 29 27 Recursively sort left half Recursively sort right half .

h> int count=0. int &b) { int temp. void swap(int &a.#include<iostream. temp = a. } //global variable declaration //function definition . b = temp.h> #include<conio. a = b.

} } } } //bubble sorting . for(i=0.i<n-1. swap(a[j].j.j<n.a[i]).i++) { for(j=i+1.void bubble(int *a.j++) { if(a[j]<a[i]) { ::count++.int n) { int i.

leftBoundary++. rightBoundary--. int rightBoundary = endIndex. array[leftBoundary]). int pivot. int endIndex) { //function definition int leftBoundary = startIndex. } swap(array[leftBoundary]. array[rightBoundary]). while(leftBoundary < rightBoundary) { while( pivot < array[rightBoundary] && rightBoundary > leftBoundary) { ::count++. } swap(array[rightBoundary]. } return leftBoundary. int startIndex. while( pivot >= array[leftBoundary] && leftBoundary < rightBoundary) { ::count++.int SplitArray(int* array. } .

splitPoint-1). if(endIndex > startIndex) { splitPoint = SplitArray(array. array[splitPoint] = pivot. endIndex). } .void QuickSort(int* array. int splitPoint. int startIndex. splitPoint+1. endIndex). int endIndex) //quick sorting { int pivot = array[startIndex]. QuickSort(array. pivot. startIndex. startIndex. QuickSort(array. }::count++.

cout<<"Enter your choice:".i<ip. cout<<"Enter numbers:".i++) cin>>a[i].i. of inputs:". cout<<"Sorting:"<<endl<<"1.1). } cout<<"After sorting"<<endl. cin>>choice. of comparisions:"<<::count<<endl. { } .int main() //function definition int *a. a=new int[ip]. break.0. int ip. getch().i++) cout<<a[i]<<endl.ip .Bubble Sort"<<endl<<"2. switch(choice) { case 1: bubble(a. cout<<"Enter no. cin>>ip.ip). return 0. for(i=0.i<ip. break.choice.Quick sort"<<endl. cout<<"Total No. case 2: QuickSort(a. for(i=0.