Welcome to Scribd. Sign in or start your free trial to enjoy unlimited e-books, audiobooks & documents.Find out more
Download
Standard view
Full view
of .
Look up keyword
Like this
2Activity
0 of .
Results for:
No results containing your search query
P. 1
Sorting in C++- Bubble Sort

Sorting in C++- Bubble Sort

Ratings: (0)|Views: 422|Likes:
Published by Lorain
This pdf explains how to do bubble sorting in C++
This pdf explains how to do bubble sorting in C++

More info:

Published by: Lorain on Oct 19, 2011
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less

09/14/2013

pdf

text

original

 
Bubble Sort
Because of bubble sort's simplicity, it is one of the oldest sorts known to man. It based on the property of a sorted listthat any two adjacent elements are in sorted order. In a typical iteration of bubble sort each adjacent pair of elementsis compared, starting with the first two elements, then the second and the third elements, and all the way to the finaltwo elements. Each time two elements are compared, if they are already in sorted order, nothing is done to them andthe next pair of elements is compared. In the case where the two elements are not in sorted order, the two elementsare swapped, putting them in order.Consider a set of data: 5 9 2 8 4 6 3. Bubble sort first compares the first two elements, the 5 and the 6. Because theyare already in sorted order, nothing happens and the next pair of numbers, the 6 and the 2 are compared. Becausethey are not in sorted order, they are swapped and the data becomes: 5 2 9 8 4 6 3. To better understand the
 
"bubbling" nature of the sort, watch how the largest number, 9, "bubbles" to the top in the first iteration of the sort.(5 9) 2 8 4 6 3 --> compare 5 and 9, no swap 5 (9 2) 8 4 6 3 --> compare 9 and 2, swap 5 2 (9 8) 4 6 3 --> compare 9and 8, swap 5 2 8 (9 4) 6 3 --> compare 9 and 4, swap 5 2 8 4 (9 6) 3 --> compare 9 and 6, swap 5 2 8 4 6 (9 3) -->compare 9 and 3, swap 5 2 8 4 6 3 9 --> first iteration completeThe bubble sort got its name because of the way the biggest elements "bubble" to the top. Notice that in the exampleabove, the largest element, the 9, got swapped all the way into its correct position at the end of the list. Asdemonstrated, this happens because in each comparison, the larger element is always pushed towards its place atthe end of the list.In the second iteration, the second-largest element will be bubbled up to its correct place in the same manner:
 
(5 2) 8 4 6 3 9 --> compare 5 and 2, swap
 
5 (2 8) 4 6 3 9 --> compare 2 and 8, no swap
 
 
5 2 (8 4) 6 3 9 --> compare 8 and 4, swap
 
5 2 4 (8 6) 3 9 --> compare 8 and 6, swap
 
5 2 4 6 (8 3) 9 --> compare 8 and 3, swap
 
5 2 4 6 3 8 9 --> no need to compare last two In the next pass through the list, the 6 would bubble up to itsposition, then the 5, the 4, the 3, and finally the 2. Here is a complete trace of the bubble sort algorithm on aten element data set:8 9 3 5 6 4 2 1 7 08 9 3 5 6 4 2 1 7 08 3 9 5 6 4 2 1 7 08 3 5 9 6 4 2 1 7 08 3 5 6 9 4 2 1 7 08 3 5 6 4 9 2 1 7 08 3 5 6 4 2 9 1 7 08 3 5 6 4 2 1 9 7 08 3 5 6 4 2 1 7 9 08 3 5 6 4 2 1 7 0 93 8 5 6 4 2 1 7 0 93 5 8 6 4 2 1 7 0 93 5 6 8 4 2 1 7 0 9
 
3 5 6 4 8 2 1 7 0 93 5 6 4 2 8 1 7 0 93 5 6 4 2 1 8 7 0 93 5 6 4 2 1 7 8 0 93 5 6 4 2 1 7 0 8 93 5 6 4 2 1 7 0 8 93 5 6 4 2 1 7 0 8 93 5 4 6 2 1 7 0 8 93 5 4 2 6 1 7 0 8 93 5 4 2 1 6 7 0 8 93 5 4 2 1 6 7 0 8 93 5 4 2 1 6 0 7 8 93 5 4 2 1 6 0 7 8 93 4 5 2 1 6 0 7 8 93 4 2 5 1 6 0 7 8 93 4 2 1 5 6 0 7 8 93 4 2 1 5 6 0 7 8 93 4 2 1 5 0 6 7 8 93 4 2 1 5 0 6 7 8 93 2 4 1 5 0 6 7 8 93 2 1 4 5 0 6 7 8 93 2 1 4 5 0 6 7 8 93 2 1 4 0 5 6 7 8 92 3 1 4 0 5 6 7 8 92 1 3 4 0 5 6 7 8 92 1 3 4 0 5 6 7 8 92 1 3 0 4 5 6 7 8 91 2 3 0 4 5 6 7 8 91 2 3 0 4 5 6 7 8 91 2 0 3 4 5 6 7 8 91 2 0 3 4 5 6 7 8 91 0 2 3 4 5 6 7 8 90 1 2 3 4 5 6 7 8 9
Sorting Terms
Comparison Function
- A function which tells whether one element is greater than, less than, or equal to another.Note that this class of functions is not limited to numbers. For example, the C Standard Library function strcmp() usesa comparison function to compare strings.
Sorting Algorithm
- An algorithm, or series of steps, that takes an unsorted list and uses a comparison function toput the list into sorted order.
Heap
- A binary tree in which each node has a greater value than its children.
Bubble Sort Problems

Activity (2)

You've already reviewed this. Edit your review.
1 thousand reads
1 hundred reads

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->