0% found this document useful (0 votes)
25 views

Sort and Search in Data Structure

The selection sort algorithm sorts an array from smallest to largest by repeatedly finding the minimum element in the unsorted portion of the array and swapping it into the sorted position. It scans the array to find the minimum value, swaps it into the first position, then scans the remaining values to find the next minimum and swaps it into the second position, continuing until the array is fully sorted.

Uploaded by

shrygpta
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Sort and Search in Data Structure

The selection sort algorithm sorts an array from smallest to largest by repeatedly finding the minimum element in the unsorted portion of the array and swapping it into the sorted position. It scans the array to find the minimum value, swaps it into the first position, then scans the remaining values to find the next minimum and swaps it into the second position, continuing until the array is fully sorted.

Uploaded by

shrygpta
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Selection Sort Algorithm

! Given an array of items, arrange the items so that they are sorted from smallest to largest. ! Select next item, in turn, that will be appended to the sorted part of the array:
Scan the array to find the smallest value, then swap this value with the value at cell 0. ! Scan the remaining values (all but the first value), to find the next smallest, then swap this value with the value at cell 1. ! Scan the remaining values (all but the first two) to find the next smallest, then swap this value with the value at cell 2. ! Continue until the array is sorted.
!

Sorting and Searching


15-110 Summer 2010 Margaret Reid-Miller

Summer 2010

15-110 (Reid-Miller)

Example: Selection Sort


[0] 5 1 1 1 1 1 1 1
Summer 2010

Selection Sort Implementation


on an array of int
public static void selectionSort(int[] data) {! data.length-1! for (int numSort = 0! numSort < _____________ ; numSort++){! _; // find the next minimum! numSort! int minPos = ________ ; // initial position of next min! numSort+1! for (int pos = __________ ; pos < data.length; pos++) {! if (data[minPos] > data[pos])! minPos = pos; // found new min! }! // swap min to next position in sorted list! int temp = data[minPos];! data[minPos] = data[ numSort! ______ ];! data[ numSort ] = temp;! ______! }!
3

[1] 1 5 5 2 2 2 2 2

[2] 3 3 3 3 3 3 3 3

[3] 7 7 7 7 7 7 7 5

[4] 2 2 2 5 5 5 5 7 find min swap to index 0 find min swap to index 1 find min swap to index 2 find min swap to index 3

15-110 (Reid-Miller)

}!

Summer 2010

15-110 (Reid-Miller)

The compareTo method


! To determine the relative ordering of two strings, the String class has a compareTo method: public int compareTo(String other)! ! By convention, the compareTo method returns a negative integer, zero, or positive integer if this object (through which the method was invoked) is less than, equal to, or greater than the object specified in the parameter, respectively. ! For example: (str1.compareTo(str2) < 0) means str1 < str2
Summer 2010 15-110 (Reid-Miller) 5

Lexicographical ordering
! The String class compareTo method compares two strings lexicographically, which is similar to alphabetically except that it includes digits and other symbols:
! ! !

Space comes before digits Digits come before uppercase letters Uppercase letters come before lower case letters

! Example: The following are in lexicographical order: "01234" "012AB" "ABC" "ABC D" "ABCD"! "XYZ" "XyZ" "abc" "bc"!
Summer 2010 15-110 (Reid-Miller) 6

Using compareTo with Strings


public void insertInArtistOrder(Song newSong) {! if (numSongs == songList.length){! !!doubleLength();! }! // Search for the location to insert in order! while (index < numSongs && newSong.getArtist().! !! compareTo(songList[index].getArtist()) > 0) {! index++;! }!
Summer 2010 15-110 (Reid-Miller) 7

Example: Date class


! Suppose a Date class is defined as follows:
!public

class Date {! !int year;! !int month;! !int day;! !! }!

! How would you write a compareTo method for the Date class?!
Summer 2010 15-110 (Reid-Miller) 8

compareTo for Date class


public class Date {!

equals for Date class


public boolean equals(Date other) {! !! return this.compareTo(other) == 0);! }! ! // other methods not shown! } // end Date class!

!public int compareTo(Date other) {! !! if (this.year != other.year)! !! return this.year - other.year;! !! else if (this.month != other.month)! !! return this.month - other.month;! !! else! !! return this.day - other.day;! !}!

Summer 2010

15-110 (Reid-Miller)

Summer 2010

15-110 (Reid-Miller)

10

Selection Sort Implementation


on an array of String objects
public static void selectionSort(String[] data) {! for (int numSort = 0; numSort < data.length-1; numSort++){! // find the next minimum! int minPos = numSort; // initial position of next min! for (int pos = numSort+1; pos < data.length; pos++) {! if (data[minPos].compareTo(data[pos]) > 0)! minPos = pos; // found new min! }! // swap in min to next position in sorted list! String temp = data[minPos];! data[minPos] = data[numSort];! data[numSort] = temporary;! }!
}!
Summer 2010 15-110 (Reid-Miller) 11

Selection Sort Implementation


on an array of Date objects
public static void selectionSort(Date[] data) {! for (int numSort = 0; numSort < data.length-1; numSort++){! // find the next minimum! int minPos = numSort; // initial position of next min! for (int pos = numSort+1; pos < data.length; pos++) {! if (data[minPos].compareTo(data[pos]) > 0)! minPos = pos; // found new min! }! // swap in min to next position in sorted list! Date temp = data[minPos];! data[minPos] = data[numSort];! data[numSort] = temp;! }!
}!
Summer 2010 15-110 (Reid-Miller) 12

See http://math.hws.edu/TMCM/java/xSortLab/ for animations

Insertion Sort Algorithm


! Given an array of items, arrange the items so that they are sorted from smallest to largest. ! For each item, in turn, insert the item into the sorted part of the array: ! The first item is sorted in a list of one item ! Insert second item in sorted list of one item. ! Insert third item in sorted list of two items. ! Insert fourth item in sorted list of three items. ! Continue until all the items are sorted. [0] 5 1 1 1 1 1 1 1
Summer 2010

Example: Insertion Sort


[1] 1 5 5 3 3 3 3 2 [2] 3 3 3 5 5 5 5 3 [3] 7 7 7 7 7 7 7 5 [4] 2 2 2 2 2 2 2 7 insert [1] shift insert [2] shift insert [3] shift insert [4] shift
14

Summer 2010

15-110 (Reid-Miller)

13

15-110 (Reid-Miller)

Insertion Sort Implementation


on an array of int
public static void insertionSort(int[] data) {! data.length! for (int numSort = 1! numSort < __________ ; numSort++){! _; // save next item to insert into sorted list! int temp = data[ numSort! ______ ];! // move the hole to insertion position ! int hole = ________;! numSort! while (________ && temp _ data[hole-1]) {! <! hole > 0! data[hole] = data[hole-1];! hole--;! }! data[hole] = temp; // insert at hole! }! }! Summer 2010
15-110 (Reid-Miller) 15

Binary Search Algorithm


! Given an array of items sorted in increasing order and an item, find the position of the item in the array: ! Guess that it is the middle item. ! If it is, then return the middle index. ! Otherwise, determine if it is in the upper or lower half. ! Repeat on this half to find in which quarter it is. ! Repeat until either find the item or there are no values left.

Summer 2010

15-110 (Reid-Miller)

16

Example: Binary Search


search for 64 [0] 11 11 11 11 11 [1] 18 18 18 18 18 [2] 19 19 19 19 19 [3] 22 22 22 22 22 [4] 24 24 24 24 24 [5] 35 35 35 35 35 [6] 37 37 37 37 37 [7] 64 64 64 64 64 [8] 68 Find middle 68 Find half 68 Find middle 68 Find half 68 Find middle Found [0] 11 11 11 11 11 11 11
Summer 2010 15-110 (Reid-Miller) 17

Example: Binary Search


search for 20 [1] 18 18 18 18 18 18 18 [2] 19 19 19 19 19 19 19 [3] 22 22 22 22 22 22 22 [4] 24 24 24 24 24 24 24 [5] 35 35 35 35 35 35 35 [6] 37 37 37 37 37 37 37 [7] 64 64 64 64 64 64 64 [8] 68 Find middle 68 Find half 68 Find middle 68 Find half 68 Find middle 68 Find half 68 Find middle Not found
18

Summer 2010

15-110 (Reid-Miller)

Binary Search Implementation


public static int binarySearch(String[] array, " String key) {! int begin = 0, end = array.length-1, mid = 0;! boolean found = false;! // key is between begin and end! while (!found && begin <= end) {! mid = (begin + end) / 2; ________________________;! // integer division! if (key.compareTo(array[mid]) == 0) ! __________;! found = true;! else if (key.compareTo(array[mid]) < 0) ! __________;! end = mid-1;! else _________;! begin = mid+1! }! if (found) return mid;! else return -1;! }!
Summer 2010 15-110 (Reid-Miller) 19

You might also like