bubble sort a best presentation topic

82
A Presentation topic for c++ topic A Good style and appreciated

Upload: saddam-hussain

Post on 07-Aug-2015

210 views

Category:

Education


0 download

TRANSCRIPT

• A Presentation topic for c++ topic• A Good style and appreciated

Sorting

Sorting takes an unordered collection and makes it an ordered one.

512354277 101

1 2 3 4 5 6

5 12 35 42 77 101

1 2 3 4 5 6

Introduction: Bubble Sort

Also called Exchange sort.It repeatedly visits the array and compares two items at a time. It swaps these two items if they are in the wrong order.It continues to visit the array until no swaps are needed thatmeans the array is sorted.

674523 14 6 3398 42

The First “Bubble Up”

674523 14 6 3398 42

The First “Bubble Up”

674523 14 6 3398 42

Swap

The First “Bubble Up”

674598 14 6 3323 42

Swap

The First “Bubble Up”

674598 14 6 3323 42

The First “Bubble Up”

674598 14 6 3323 42

Swap

The First “Bubble Up”

679845 14 6 3323 42

Swap

The First “Bubble Up”

679845 14 6 3323 42

The First “Bubble Up”

679845 14 6 3323 42

Swap

The First “Bubble Up”

671445 98 6 3323 42

Swap

The First “Bubble Up”

671445 98 6 3323 42

The First “Bubble Up”

671445 98 6 3323 42

Swap

The First “Bubble Up”

671445 6 98 3323 42

Swap

The First “Bubble Up”

671445 6 98 3323 42

The First “Bubble Up”

671445 6 98 3323 42

Swap

The First “Bubble Up”

981445 6 67 3323 42

Swap

The First “Bubble Up”

981445 6 67 3323 42

The First “Bubble Up”

981445 6 67 3323 42

Swap

The First “Bubble Up”

331445 6 67 9823 42

Swap

The First “Bubble Up”

331445 6 67 9823 42

The First “Bubble Up”

331445 6 67 9823 42

Swap

The First “Bubble Up”

331445 6 67 4223 98

Swap

The First “Bubble Up”

331445 6 67 4223 98

Finished first “Bubble Up”

The First “Bubble Up”

The Second “Bubble Up”

331445 6 67 4223 98

The Second “Bubble Up”

331445 6 67 4223 98

No Swap

The Second “Bubble Up”

331445 6 67 4223 98

The Second “Bubble Up”

331445 6 67 4223 98

Swap

The Second “Bubble Up”

334514 6 67 4223 98

Swap

The Second “Bubble Up”

334514 6 67 4223 98

The Second “Bubble Up”

334514 6 67 4223 98

Swap

The Second “Bubble Up”

33614 45 67 4223 98

Swap

The Second “Bubble Up”

33614 45 67 4223 98

The Second “Bubble Up”

33614 45 67 4223 98

No Swap

The Second “Bubble Up”

33614 45 67 4223 98

The Second “Bubble Up”

33614 45 67 4223 98

Swap

The Second “Bubble Up”

67614 45 33 4223 98

Swap

The Second “Bubble Up”

67614 45 33 4223 98

The Second “Bubble Up”

67614 45 33 4223 98

Swap

The Second “Bubble Up”

42614 45 33 6723 98

Swap

42614 45 33 6723 98

Finished second “Bubble Up”

The Second “Bubble Up”

The Third “Bubble Up”

42614 45 33 6723 98

The Third “Bubble Up”

42614 45 33 6723 98

Swap

The Third “Bubble Up”

42623 45 33 6714 98

Swap

The Third “Bubble Up”

42623 45 33 6714 98

The Third “Bubble Up”

42623 45 33 6714 98

Swap

The Third “Bubble Up”

42236 45 33 6714 98

Swap

The Third “Bubble Up”

42236 45 33 6714 98

The Third “Bubble Up”

42236 45 33 6714 98

No Swap

The Third “Bubble Up”

42236 45 33 6714 98

The Third “Bubble Up”

42236 45 33 6714 98

Swap

The Third “Bubble Up”

42236 33 45 6714 98

Swap

The Third “Bubble Up”

42236 33 45 6714 98

The Third “Bubble Up”

42236 33 45 6714 98

Swap

The Third “Bubble Up”

45236 33 42 6714 98

Swap

After Third Pass of Outer Loop

45236 33 42 6714 98

Finished third “Bubble Up”

The Fourth “Bubble Up”

45236 33 42 6714 98

The Fourth “Bubble Up”

45236 33 42 6714 98

Swap

The Fourth “Bubble Up”

452314 33 42 676 98

Swap

The Fourth “Bubble Up”

452314 33 42 676 98

The Fourth “Bubble Up”

452314 33 42 676 98

No Swap

The Fourth “Bubble Up”

452314 33 42 676 98

The Fourth “Bubble Up”

452314 33 42 676 98

No Swap

The Fourth “Bubble Up”

452314 33 42 676 98

The Fourth “Bubble Up”

452314 33 42 676 98

No Swap

452314 33 42 676 98

Finished fourth “Bubble Up”

After Fourth Pass of Outer Loop

The Fifth “Bubble Up”

452314 33 42 676 98

The Fifth “Bubble Up”

452314 33 42 676 98

No Swap

The Fifth “Bubble Up”

452314 33 42 676 98

The Fifth “Bubble Up”

452314 33 42 676 98

No Swap

The Fifth “Bubble Up”

452314 33 42 676 98

The Fifth “Bubble Up”

452314 33 42 676 98

No Swap

After Fifth Pass of Outer Loop

452314 33 42 676 98

Finished fifth “Bubble Up”

452314 33 42 676 98

We did not do any swapping,so all of the other elementsmust be correctly placed.

We can “skip” the last twopasses of the outer loop.

Program

void main(){ int arr[5],i,j,tem; cout<<“Enter Five Values”; for(i=0,i<5,i++) cin>>arr[i];

cout<<“The origanal values in array:\n”; for(i=0;i<5;i++) cout<<arr[i]<<“ “;

for(i=0;i<5;i++) for(j=0;j<4;j++)If(arr[ j ]>arr[ j+1 ]{ tem=arr[ j ]; arr[ j ]=arr[ j+1 ]; arr[ j+1]=tem;} cout<<“\n The sorted array: \n”; for(i=0;i<5;i++) cout<<arr[ i ]<<“ “; getch();}