Download presentation

Presentation is loading. Please wait.

Published byJessica Emery Modified over 4 years ago

1
CS126 ZV 2005 Exchange Sort(Bubble Sort)1 Zabin Visram Room CS115 Lecture Structure References Exchange Sort (bubble Sort)

2
CS126 ZV 2005 Exchange Sort(Bubble Sort)2 Exchange sort Basic operation of Exchange sort is the exchange of adjacent pair of elements Overall sort consist of a number of passes over the data Each pass starts at one end of the array and works toward the other end Each pair of elements that are out of order are exchanged

3
CS126 ZV 2005 Exchange Sort(Bubble Sort)3 Exchange Sort 390250250250250 250390182182182 1821823904545 454545390235 235235235235390 (a) (b) (c ) (d) (e) The first pass of an exchange sort (a) In first pass 390 is compared with 205; then exchanged as 250 is smaller (b) 390 is compared with 182, they exchanged. Result shown in (c ) In (c )390 is compared with 45, they exchanged Then in (d) 390 is compared with 235 and exchanged Result is (e) – regarding the columns as arrays - element 390 has bubbled from index 0 to highest index (4)

4
CS126 ZV 2005 Exchange Sort(Bubble Sort)4 bubbling The elements with largest value are moving slowly or bubbling to the top If no exchanges are made during one pass over the data, the data have been sorted and process terminates

5
CS126 ZV 2005 Exchange Sort(Bubble Sort)5 Analysis The first pass moves the largest element to the nth, forming a sorted list of one. The second pass has to consider only n-1 elements The second pass moves the second largest element to the n-1 position. Therefore the third pass only needs to consider n-2 elements and so on

6
CS126 ZV 2005 Exchange Sort(Bubble Sort)6 public static void bubbleSort(int data[], int n) { //pre: 0 <= n <= data.length //post: values in data[0..n-1] are in //ascending order int numSorted = 0; //number of values //in order while (numSorted < n) { //bubble a large element to correct //position for (int index = 1; index<numSorted; index++) if (dat[index] < data[index –1]) swap(data, index, index –1); //at least one more value in place numSorted++; }

7
CS126 ZV 2005 Exchange Sort(Bubble Sort)7 public static void bubbleSort(int data[], int n) { //pre: 0 <= n <= data.length //post: values in data[0..n-1] are in //ascending order int numSorted = 0; //number of values //in order while (numSorted < n) { //bubble a large element to correct //position for (int index = 1; index<numSorted; index++) if (dat[index] < data[index –1]) swap(data, index, index –1); //at least one more value in place numSorted++; } public static void swap(int data[], int i, int j) { //pre: 0 <= i, j <= data.length //post: data[i] and data[j] are exchanged int temp; temp = data[i]; data[i] = data[j]; data[j] = temp; }

8
CS126 ZV 2005 Exchange Sort(Bubble Sort)8 Disadvantages Two disadvantages of this sort are: (i) the use of swap (requiring three assignments) within the inner loop; (ii) the fact that moves are never by more than one position at a time. (Compare selection sort which has no moves in the inner loop and elements may move large distances.)

9
CS126 ZV 2005 Exchange Sort(Bubble Sort)9 Demonstration of Bubble Sort http://www.cosc.canterbury.ac.nz/people/ mukundan/dsal/BSort.html http://www.cosc.canterbury.ac.nz/people/ mukundan/dsal/BSort.html

Similar presentations

OK

Sorting A fundamental operation in computer science (many programs need to sort as an intermediate step). Many sorting algorithms have been developed Choose.

Sorting A fundamental operation in computer science (many programs need to sort as an intermediate step). Many sorting algorithms have been developed Choose.

© 2019 SlidePlayer.com Inc.

All rights reserved.

To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie policy.

Ads by Google