sorting an array

Post on 08-Jan-2016

86 Views

Category:

Documents

2 Downloads

Preview:

Click to see full reader

DESCRIPTION

Sorting an array. bubble and selection sorts. Sorting. An arrangement or permutation of data May be either: ascending (non decreasing) descending (non increasing). Bubble sort. Bubble sort algorithm. Compare adjacent pairs of array elements Swap if necessary (out of order) - PowerPoint PPT Presentation

TRANSCRIPT

Sorting an arraySorting an array

bubble and selection sortsbubble and selection sorts

SortingSorting

An arrangement or permutation of An arrangement or permutation of datadata

May be either:May be either:– ascending (non decreasing)ascending (non decreasing)– descending (non increasing)descending (non increasing)

Bubble sortBubble sort

Bubble sort algorithmBubble sort algorithm

1.1. Compare adjacent pairs of array Compare adjacent pairs of array elementselements

2.2. Swap if necessary (out of order)Swap if necessary (out of order)

3.3. Repeat on next pairRepeat on next pair

Bubble sort exampleBubble sort example Given (D,B,E,C,A)Given (D,B,E,C,A) We want (A,B,C,D,E)We want (A,B,C,D,E)

– Smallest to largest; ascending; non decreasingSmallest to largest; ascending; non decreasing Pass 1:Pass 1:

D,B,E,C,AD,B,E,C,A startstartDD,,BB,E,C,A,E,C,A comparecompareBB,,DD,E,C,A,E,C,A swapswapB,B,DD,,EE,C,A,C,A comparecompareB,B,DD,,EE,C,A,C,A don’t swapdon’t swapB,D,B,D,EE,,CC,A,A comparecompareB,D,B,D,CC,,EE,A,A swapswapB,D,C,B,D,C,EE,,AA comparecompareB,D,C,B,D,C,AA,,EE swapswap

Note that at the end of pass 1, the last element (E above) Note that at the end of pass 1, the last element (E above) is the largest.is the largest.

Bubble sort exampleBubble sort example Pass 2:Pass 2:

B,D,C,A,EB,D,C,A,E start (from pass 1)start (from pass 1)BB,,DD,C,A,E,C,A,E comparecompareBB,,DD,C,A,E,C,A,E don’t swapdon’t swapB,B,DD,,CC,A,E,A,E comparecompareB,B,CC,,DD,A,E,A,E swapswapB,C,B,C,DD,,AA,E,E comparecompareB,C,B,C,AA,,DD,E,E swapswap

Note that we don’t need to continue and compare Note that we don’t need to continue and compare D and E because E was the overall maximum.D and E because E was the overall maximum.

Further note that D is the max of the sub array Further note that D is the max of the sub array considered in pass 2.considered in pass 2.

Bubble sort exampleBubble sort example

Pass 3:Pass 3:B,C,A,D,EB,C,A,D,E start (from pass 2)start (from pass 2)

BB,,CC,A,D,E,A,D,E comparecompare

BB,,CC,A,D,E,A,D,E don’t swapdon’t swap

B,B,CC,,AA,D,E,D,E comparecompare

B,B,AA,,CC,D,E,D,E swapswap

Note that we don’t need to continue.Note that we don’t need to continue.

Bubble sort exampleBubble sort example

Pass 4:Pass 4:B,A,C,D,EB,A,C,D,E start (from pass 3)start (from pass 3)BB,,AA,C,D,E,C,D,E comparecompareAA,,BB,C,D,E,C,D,E swapswap

Done!Done!

In general, if the length of the list is N, we In general, if the length of the list is N, we need to make N-1 passes.need to make N-1 passes.

Coding the bubble sortCoding the bubble sort

First, we need the swap operation:First, we need the swap operation:

1,2,4,3,5 becomes 1,2,3,4,51,2,4,3,5 becomes 1,2,3,4,5

Write a function that given the index Write a function that given the index i of an array element swaps it with i of an array element swaps it with the element at index i+1.the element at index i+1.

Coding the bubble sortCoding the bubble sort

public static void swap ( int start, int[] public static void swap ( int start, int[] values ) {values ) {

……

}}

Coding the bubble sortCoding the bubble sort

public static void swap ( int start, int[] public static void swap ( int start, int[] values ) {values ) {

int temp = values[ start ];int temp = values[ start ];

……

}}

Coding the bubble sortCoding the bubble sort

public static void swap ( int start, int[] public static void swap ( int start, int[] values ) {values ) {

int temp = values[ start ];int temp = values[ start ];

values[ start ] = values[ start + 1 ];values[ start ] = values[ start + 1 ];

……

}}

Coding the bubble sortCoding the bubble sort

public static void swap ( int start, int[] public static void swap ( int start, int[] values ) {values ) {

int temp = values[ start ];int temp = values[ start ];

values[ start ] = values[ start + 1 ];values[ start ] = values[ start + 1 ];

values[ start + 1 ] = temp;values[ start + 1 ] = temp;

}}

Coding the bubble sortCoding the bubble sort

Now we need a function to call swap when Now we need a function to call swap when that operation is necessary.that operation is necessary.

public static void bubblesort ( int[] values ) {public static void bubblesort ( int[] values ) {

……

}}

Recall: In general, if the length of the list is Recall: In general, if the length of the list is N, we need to make N-1 passes.N, we need to make N-1 passes.

Coding the bubble sortCoding the bubble sortRecall: In general, if the length of the list is N, we need to make N-1 passes.Recall: In general, if the length of the list is N, we need to make N-1 passes.

public static void bubblesort ( int[] values ) {public static void bubblesort ( int[] values ) {//perform one pass through the array//perform one pass through the arrayfor (int i=0; i<values.length; i++) {for (int i=0; i<values.length; i++) {

……}}

}}

Recall from our example:Recall from our example:Pass 1:Pass 1:D,B,E,C,AD,B,E,C,A startstartDD,,BB,E,C,A,E,C,A comparecompareBB,,DD,E,C,A,E,C,A swapswapB,B,DD,,EE,C,A,C,A comparecompareB,B,DD,,EE,C,A,C,A don’t swapdon’t swapB,D,B,D,EE,,CC,A,A comparecompareB,D,B,D,CC,,EE,A,A swapswapB,D,C,B,D,C,EE,,AA comparecompareB,D,C,B,D,C,AA,,EE swapswap

Unlike the selection sort where we most avoid repeatedly starting at the beginning in the inner loop each time, the bubble sort can repeatedly process the entire array in the inner loop.

Later, we will avoid this for reasons of efficiency.

Coding the bubble sortCoding the bubble sortpublic static void bubblesort ( int[] values ) {public static void bubblesort ( int[] values ) {

//perform one pass through the array//perform one pass through the arrayfor (int i=0; i<values.length; i++) {for (int i=0; i<values.length; i++) {

for (int j=0; j<values.lengthfor (int j=0; j<values.length-1-1; j++) {; j++) {

// swap if necessary// swap if necessary……

}}}}

}}

Recall from our example:Recall from our example:Pass 1:Pass 1:D,B,E,C,AD,B,E,C,A startstartDD,,BB,E,C,A,E,C,A comparecompareBB,,DD,E,C,A,E,C,A swapswapB,B,DD,,EE,C,A,C,A comparecompareB,B,DD,,EE,C,A,C,A don’t swapdon’t swapB,D,B,D,EE,,CC,A,A comparecompareB,D,B,D,CC,,EE,A,A swapswapB,D,C,B,D,C,EE,,AA comparecompareB,D,C,B,D,C,AA,,EE swapswap

Note that we need to shorten up by one for the last pair.

Coding the bubble sortCoding the bubble sortpublic static void bubblesort ( int[] values ) {public static void bubblesort ( int[] values ) {

//perform one pass through the array//perform one pass through the arrayfor (int i=0; i<values.length; i++) {for (int i=0; i<values.length; i++) {

for (int j=0; j<values.length-1; j++) {for (int j=0; j<values.length-1; j++) {// swap if necessary// swap if necessaryif (values[j] > values[j+1]) {if (values[j] > values[j+1]) {

swap( j, values );swap( j, values );}}

}}}}

}}

Recall from our example:Recall from our example:Pass 1:Pass 1:D,B,E,C,AD,B,E,C,A startstartDD,,BB,E,C,A,E,C,A comparecompareBB,,DD,E,C,A,E,C,A swapswapB,B,DD,,EE,C,A,C,A comparecompareB,B,DD,,EE,C,A,C,A don’t swapdon’t swapB,D,B,D,EE,,CC,A,A comparecompareB,D,B,D,CC,,EE,A,A swapswapB,D,C,B,D,C,EE,,AA comparecompareB,D,C,B,D,C,AA,,EE swapswap

Note that we need to shorten up by one for the last pair.

See why that was necessary?

Coding the bubble sortCoding the bubble sortBut wait! The inner loop (j) always goes from start to But wait! The inner loop (j) always goes from start to

end but we said that each pass ensures that the last end but we said that each pass ensures that the last element in the sub array is maximal and we don’t element in the sub array is maximal and we don’t need to check so far each time.need to check so far each time.

public static void bubblesort ( int[] values ) {public static void bubblesort ( int[] values ) {//perform one pass through the array//perform one pass through the arrayfor (int i=0; i<values.length; i++) {for (int i=0; i<values.length; i++) {

for (int j=0; j<values.length-1; j++) {for (int j=0; j<values.length-1; j++) {// swap if necessary// swap if necessaryif (values[j] > values[j+1]) {if (values[j] > values[j+1]) {

swap( j, values );swap( j, values );}}

}}}}

}}

Coding the bubble sortCoding the bubble sortBut wait! The inner loop (j) always goes from start to end but But wait! The inner loop (j) always goes from start to end but

we said that each pass ensures that the last element in the we said that each pass ensures that the last element in the sub array is maximal and we don’t need to check so far sub array is maximal and we don’t need to check so far each time.each time.

public static void bubblesort ( int[] values ) {public static void bubblesort ( int[] values ) {//perform one pass through the array//perform one pass through the arrayfor (int i=0; i<values.length; i++) {for (int i=0; i<values.length; i++) {

for (int j=0; j<values.length-1for (int j=0; j<values.length-1-i-i; j++) {; j++) {

// swap if necessary// swap if necessaryif (values[j] > values[j+1]) {if (values[j] > values[j+1]) {

swap( j, values );swap( j, values );}}

}}}}

}}

Unlike the selection sort where we most avoid repeatedly starting at the beginning in the inner loop each time, the bubble sort can repeatedly process the entire array in the inner loop.

Here, we avoid this for reasons of efficiency.

Selection sortSelection sort

Selection sortSelection sort

Based on the idea of repeatedly Based on the idea of repeatedly finding the minimal elements.finding the minimal elements.

How can we find the (single, most) How can we find the (single, most) minimal element in an array?minimal element in an array?

Selection sortSelection sort

How can we find the (single, most) minimal element How can we find the (single, most) minimal element in an array?in an array?

……//let 0 be the location of the smallest element so far//let 0 be the location of the smallest element so farint whereSmallest = 0;int whereSmallest = 0;for (int i=1; i<A.length; i++) {for (int i=1; i<A.length; i++) {

if (A[i]<A[whereSmallest]) {if (A[i]<A[whereSmallest]) {whereSmallest = i;whereSmallest = i;

}}}}System.out.println( "the smallest is " + A[whereSmallest]System.out.println( "the smallest is " + A[whereSmallest]

+ " which was located at position " + whereSmallest + "." + " which was located at position " + whereSmallest + "." ););

Selection sortSelection sort

Idea:Idea:– Find the smallest in A[0]..A[ A.length-1 ].Find the smallest in A[0]..A[ A.length-1 ].– Put that in A[0].Put that in A[0].– Then find the smallest in A[1]..A[ A.length-1 ].Then find the smallest in A[1]..A[ A.length-1 ].– Put that in A[1].Put that in A[1].– ……

– But first, let’s develop a swapPairs function that But first, let’s develop a swapPairs function that swaps a pair of elements denoted by a and b in swaps a pair of elements denoted by a and b in some array, A.some array, A.

<-1, 2, 4, -5, 12><-1, 2, 4, -5, 12> <-5, 2, 4, -1, 12><-5, 2, 4, -1, 12>

Selection sortSelection sort

public static void swapPair ( … ) {public static void swapPair ( … ) {

……

}}

What do we need in here to do the job (function parameters)?

Selection sortSelection sort

public static void swapPair ( public static void swapPair ( int[] A, int a, int bint[] A, int a, int b ) )

{{

……

}}

What do we need in here to do the job (function parameters)?

Selection sortSelection sort

public static void swapPair ( public static void swapPair ( int[] A, int a, int bint[] A, int a, int b ) )

{{

int temp = A[a];int temp = A[a];

A[a] = A[b];A[a] = A[b];

A[b] = temp;A[b] = temp;

}}

Selection sortSelection sort Idea:Idea:

– Find the smallest in A[0]..A[ A.length-1 ].Find the smallest in A[0]..A[ A.length-1 ].– Put that in A[0].Put that in A[0].– Then find the smallest in A[1]..A[ A.length-1 ].Then find the smallest in A[1]..A[ A.length-1 ].– Put that in A[1].Put that in A[1].

……//let 0 be the location of the smallest element so far//let 0 be the location of the smallest element so farint whereSmallest = 0;int whereSmallest = 0;for (int i=1; i<A.length; i++) {for (int i=1; i<A.length; i++) {

if (A[i]<A[whereSmallest]) {if (A[i]<A[whereSmallest]) {whereSmallest = i;whereSmallest = i;

}}}}swapPairs( A, 0, whereSmallest );swapPairs( A, 0, whereSmallest );

Selection sortSelection sort Idea:Idea:

– Find the smallest in A[0]..A[ A.length-1 ].Find the smallest in A[0]..A[ A.length-1 ].– Put that in A[0].Put that in A[0].– Then find the smallest in A[1]..A[ A.length-1 ].Then find the smallest in A[1]..A[ A.length-1 ].– Put that in A[1].Put that in A[1].

……for (int j=0; j<A.length; j++) {for (int j=0; j<A.length; j++) {

//let j be the location of the smallest element so far//let j be the location of the smallest element so farint whereSmallest = int whereSmallest = jj;;for (int i=for (int i=j+j+1; i<A.length; i++) {1; i<A.length; i++) {

if (A[i]<A[whereSmallest]) {if (A[i]<A[whereSmallest]) {whereSmallest = i;whereSmallest = i;

}}}}swap( A, swap( A, jj, whereSmallest );, whereSmallest );

}}

top related