Вы находитесь на странице: 1из 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, Scan back from the end until you find the first card larger than the new one, Move all the lower ones up one slot insert it

10

From the first element


Exchange pairs if theyre 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,b) { int t; t=a; a=b; b=t; } void bubble( int a[], int n ) { int i, j; for(i=0;i<n;i++) { /* n passes thru the array */ /* From start to the end of unsorted part */ for(j=1;j<(n-i);j++) { /* If adjacent items out of order, swap */ if( a[j-1]>a[j] ) SWAP(a[j-1],a[j]); } } }

/* Bubble sort for integers */ #define SWAP(a,b) { int t; t=a; a=b; b=t; }

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

/* Bubble sort for integers */ #define SWAP(a,b) { int t; t=a; a=b; b=t; }

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

/* Bubble sort for integers */ #define SWAP(a,b) { int t; t=a; a=b; b=t; }

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

/* Bubble sort for integers */ #define SWAP(a,b) { int t; t=a; a=b; b=t; }

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

Efficient sorting algorithm


Discovered by C.A.R. Hoare

Example of Divide and Conquer algorithm Two phases


Partition phase
Divides the work into half

Sort phase
Conquers the halves!

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

Implementation

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

int partition( int *a, int low, int high ) { int left, right; int pivot_item; pivot_item = a[low]; pivot = left = low; right = high; 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); } /* right is final position for the pivot */ a[low] = a[right]; a[right] = pivot_item; return right; }

int partition( int *a, int low, int high ) { int left, right; int pivot_item; pivot_item = a[low]; pivot = left = low; right = high; Any item will do as the pivot, while ( left < right ) { choose the leftmost one! /* 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); } 23 12 15 38 42 18 36 29 27 /* right is final position for the pivot */ a[low] = a[right]; a[right] = pivot_item; return right; low high }

This example uses ints to keep things simple!

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

int partition( int *a, int low, int high ) {


int left, right; int pivot_item; pivot_item = a[low]; pivot = left = low; right = high;

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); left } /* right is final position for the pivot */ a[low] = a[right]; 23 12 15 38 42 18 36 29 a[right] = pivot_item; return right; low pivot: 23 }

right 27

high

int partition( int *a, int low, int high ) {


int left, right; int pivot_item; pivot_item = a[low]; pivot = left = low; right = high;

Move the left pointer while it points to items <= pivot 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); left right } Move right /* right is final position for the pivot */ similarly a[low] = a[right]; 23 12 15 38 42 18 36 29 27 a[right] = pivot_item; return right; } low high pivot: 23

int partition( int *a, int low, int high ) {


int left, right; int pivot_item; pivot_item = a[low]; pivot = left = low; right = high;

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++;


/* Move right while item > pivot */

while( a[right] >= pivot_item ) right--; if ( left < right ) SWAP(a,left,right); } left right /* right is final position for the pivot */ a[low] = a[right]; a[right] = pivot_item; 23 12 15 38 42 18 36 29 27 return right; pivot: 23 } low high

int partition( int *a, int low, int high ) {


int left, right; int pivot_item; pivot_item = a[low]; pivot = left = low; right = high;

left and right have swapped over, so stop

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); } right left /* right is final position for the pivot */ a[low] = a[right]; a[right] = pivot_item; 23 12 15 18 42 38 36 29 27 return right; } low high pivot: 23

int partition( int *a, int low, int high ) {


int left, right; int pivot_item; pivot_item = a[low]; pivot = left = low; right = high; right left

while ( left < right ) {


/* Move left while item < pivot */

23

12while( 15 a[left] 18 42 <= 38 pivot_item 36 29 ) 27left++;


/* Move right while item > pivot */

while( a[right] >= pivot_item ) right--; if ( left < :right low high pivot 23 ) SWAP(a,left,right); } /* right is final position for the pivot */ a[low] = a[right]; Finally, swap the pivot a[right] = pivot_item; and right return right; }

int partition( int *a, int low, int high ) {


int left, right; int pivot_item; pivot_item = a[low]; pivot = left = low; right = high; right

while ( left < right ) { 18

pivot: 23 12while( 15 a[left] 23 42 <= 38 pivot_item 36 29 ) 27left++;


/* Move left while item < pivot */ /* Move right while item > pivot */

while( a[right] >= pivot_item ) right--; if ( left < right ) SWAP(a,left,right); low high } /* right is final position for the pivot */ a[low] = a[right]; Return the position a[right] = pivot_item; of the pivot return right; }

pivot

pivot: 23 18 12 15 23 42 38 36 29 27

Recursively sort left half

Recursively sort right half

#include<iostream.h> #include<conio.h> int count=0; void swap(int &a, int &b) { int temp; temp = a; a = b; b = temp; }

//global variable declaration //function definition

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

//bubble sorting

int SplitArray(int* array, int pivot, int startIndex, int endIndex) {

//function definition

int leftBoundary = startIndex; int rightBoundary = endIndex; while(leftBoundary < rightBoundary) { while( pivot < array[rightBoundary] && rightBoundary > leftBoundary) { ::count++; rightBoundary--; } swap(array[leftBoundary], array[rightBoundary]); while( pivot >= array[leftBoundary] && leftBoundary < rightBoundary) { ::count++; leftBoundary++; } swap(array[rightBoundary], array[leftBoundary]); } return leftBoundary; }

void QuickSort(int* array, int startIndex, int endIndex) //quick sorting { int pivot = array[startIndex]; int splitPoint; if(endIndex > startIndex)

{
splitPoint = SplitArray(array, pivot, startIndex, endIndex); array[splitPoint] = pivot; QuickSort(array, startIndex, splitPoint-1); QuickSort(array, splitPoint+1, endIndex); }::count++; }

int main()

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

Вам также может понравиться