searching & sortingerman/cs101/set13.pdf · bubble sort •array divided into two sublists:...

14
Searching & Sorting

Upload: others

Post on 15-Aug-2020

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Searching & Sortingerman/CS101/Set13.pdf · Bubble Sort •Array divided into two sublists: sortedand unsorted. •The largest element is bubbled from the unsorted part and moved

Searching & Sorting

Page 2: Searching & Sortingerman/CS101/Set13.pdf · Bubble Sort •Array divided into two sublists: sortedand unsorted. •The largest element is bubbled from the unsorted part and moved

Searching

•Search the first n elements of x for a target value & return its location if found, else -1

•public static int search(int n, int[] x, int target)

Page 3: Searching & Sortingerman/CS101/Set13.pdf · Bubble Sort •Array divided into two sublists: sortedand unsorted. •The largest element is bubbled from the unsorted part and moved

Searching

•Search the first n elements of x for a target value & return its location if found, else -1

public static int search(int n, int[] x, int target) {

int pos = 0;

while ( pos < n )

pos++;

if ( x[pos] == target)

return pos; // found at pos

}

Sequential search O(n)

Page 4: Searching & Sortingerman/CS101/Set13.pdf · Bubble Sort •Array divided into two sublists: sortedand unsorted. •The largest element is bubbled from the unsorted part and moved

Searching

•Search the first n elements of x for a target value & return its location if found, else -1

•Complexity?

•More efficient solution?

Page 5: Searching & Sortingerman/CS101/Set13.pdf · Bubble Sort •Array divided into two sublists: sortedand unsorted. •The largest element is bubbled from the unsorted part and moved

Binary Search

Page 6: Searching & Sortingerman/CS101/Set13.pdf · Bubble Sort •Array divided into two sublists: sortedand unsorted. •The largest element is bubbled from the unsorted part and moved

Binary Search

public static void binarySearch(int[ ] array, int lowerbound, int upperbound, int key){

int position;

// To start, find the index of the middle position.position = ( lowerbound + upperbound) / 2;

while((array[position] != key) && (lowerbound <= upperbound)){

if (array[position] > key) // If the number is > key, decrease position by one.{

upperbound = position - 1;}

else{

lowerbound = position + 1; // Else, increase position by one.}

position = (lowerbound + upperbound) / 2;}if (lowerbound <= upperbound){

System.out.println("The number was found in array index" + position);

}else

System.out.println("Sorry, the number is not in this array. ");}

}

Page 7: Searching & Sortingerman/CS101/Set13.pdf · Bubble Sort •Array divided into two sublists: sortedand unsorted. •The largest element is bubbled from the unsorted part and moved

Sorting

Page 8: Searching & Sortingerman/CS101/Set13.pdf · Bubble Sort •Array divided into two sublists: sortedand unsorted. •The largest element is bubbled from the unsorted part and moved

Sorting

• Selection sort

591734

541739

541379

341579

314579

134579

n=6 n=5 n=4 n=3 n=2 n=1

Sum = n.( n +1) / 2 = n2/2 + n/2

+ + + + +

O( n2)

Page 9: Searching & Sortingerman/CS101/Set13.pdf · Bubble Sort •Array divided into two sublists: sortedand unsorted. •The largest element is bubbled from the unsorted part and moved

Selection Sort

• To sort first n elements of array X

while n > 1

find location of max value in first n elements of X

swap element at location of max with n’th element

decrement n

9 1 7 35

1 2 3 40

X

tmp 9

3 9

swap( int i, int j) {

int tmp;

tmp = X[i];

X[i} = X[j]:

X[j] = tmp;

}

Page 10: Searching & Sortingerman/CS101/Set13.pdf · Bubble Sort •Array divided into two sublists: sortedand unsorted. •The largest element is bubbled from the unsorted part and moved

Selection Sort (alternative)public class SelectionSort {

private static void swap(int[] a, int i, int j) {

int temp = a[i];

a[i] = a[j];

a[j] = temp;

}

public static int[] selectionSort(int[] list) {

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

// Find the index of the minimum value

int minPos = i;

for (int j = i + 1; j < list.length; j++) {

if (list[j] < list[minPos]) {

minPos = j;

}

}

swap(list, minPos, i);

}

return list;

}

Page 11: Searching & Sortingerman/CS101/Set13.pdf · Bubble Sort •Array divided into two sublists: sortedand unsorted. •The largest element is bubbled from the unsorted part and moved

Sorting

•Complexity?

•What if the array is already sorted?

•More efficient techniques?

Page 12: Searching & Sortingerman/CS101/Set13.pdf · Bubble Sort •Array divided into two sublists: sortedand unsorted. •The largest element is bubbled from the unsorted part and moved

Fall 2015 CS202 - Fundamentals of Computer Science II 12

Bubble Sort

• Array divided into two sublists: sorted and unsorted.

• The largest element is bubbled from the unsorted part and moved to thesorted part.

• After that, the wall moves one element back, increasing the number of sortedelements and decreasing the number of unsorted ones.

• One sort pass: each time an element moves from the unsorted part to thesorted part.

• Given a list of n elements, bubble sort requires up to n-1 passes (maximumpasses) to sort data.

Page 13: Searching & Sortingerman/CS101/Set13.pdf · Bubble Sort •Array divided into two sublists: sortedand unsorted. •The largest element is bubbled from the unsorted part and moved

Fall 2015 CS202 - Fundamentals of Computer Science II 13

Bubble Sort (cont.)

Page 14: Searching & Sortingerman/CS101/Set13.pdf · Bubble Sort •Array divided into two sublists: sortedand unsorted. •The largest element is bubbled from the unsorted part and moved

Bubble Sort

public static void BubbleSort( int [ ] num ) {

boolean flag = true; // set flag to true to begin first pass

int temp; //holding variable

while ( flag ) {

flag= false; //set flag to false awaiting a possible swap

for( j=0; j < num.length -1; j++ ) {

if ( num[ j ] > num[j+1] ) { // change to < for descending sort

temp = num[ j ]; //swap elements

num[ j ] = num[ j+1 ];

num[ j+1 ] = temp;

flag = true; //shows a swap occurred

}

}

}

}