160 likes | 318 Views
15 Sorting. CE00858-1: Fundamental Programming Techniques. August 14. 1. Objectives . In this session, we will develop a sorting algorithm use this sort in a problem. August 14. 2. Sorting. stored data frequently needs to be sorted into order
E N D
15 Sorting CE00858-1: Fundamental Programming Techniques August 14 15 Sorting 1
Objectives In this session, we will develop a sorting algorithm use this sort in a problem August 14 15 Sorting 2
Sorting stored data frequently needs to be sorted into order most simple sorts go through the data a number of times after each pass through the data, one more element is in the correct place August 14 15 Sorting 3
Selection sort • selection sort is simple and intuitive • each pass through the data: • find largest element in the unsorted part of data • swap this largest element with the element in the last position of the unsorted data 15 Sorting
Selection sort demonstration pass 1: check data in locations 0 to 4 9 in location 0 is largest swap with element in location 4 9 8 6 2 5 5 8 6 2 9 5 2 6 8 9 5 2 6 8 9 2 5 6 8 9 pass 2: check data in locations 0 to 3 8 in location 1 is largest swap with element in location 3 pass 3: check data in locations 0 to 2 6 in location 2 is largest in correct location – no swap pass 4: check data in locations 0 to 1 5 in location 0 is largest swap with element in location 1 for any pass: check data in locations 0 to length – pass find largest swap largest with element in length - pass 15 Sorting
Selection sort analysis what operations are performed? iteration to pass through data length – 1 times what operations are repeated inside the loop? find largest value in unsorted part of array if item not in correct location swap largest value with last unsorted item August 14 15 Sorting 6
Selection sort method public static void sort(int[] arr) { for (int pass = 1; pass < arr.length; pass++) { int largestPos = findLargest(arr, arr.length - pass); if (largestPos != arr.length - pass) { swap(arr, largestPos, arr.length - pass); } output(arr); //this is for testing purposes only } } August 14 15 Sorting 7
Find largest method public static int findLargest(int [] arr, int num) { int largestPos = 0; for (int i = 1; i <= num; i++) { if (arr[i] > arr[largestPos]) { largestPos = i; } } return largestPos; } August 14 15 Sorting 8
Swap method public static void swap(int [] arr, int first, int second) { int temp = arr[first]; arr[first] = arr[second]; arr[second] = temp; } August 14 15 Sorting 9
Sorting example - ExamResults • program to: • read in 20 integer exam results • sort the results into ascending order • output the sorted results • no validation is required 15 Sorting
Exam results analysis • what data is used? • arr: integer array to hold exam results input by user • largestPos: integer to hold position of largest value • what operations are performed? • iteration to input data • sort data – as above • iteration to output data 15 Sorting
what is done once before the loop to input data? • create Scanner object • create empty array • how many times is loop to input data repeated? • 20 times, i = 0, length of array • what operations are repeated inside the loop to input data? • input value into next array location • what operations are done once between loop to input data and loop to output data? • sort data • how many times is loop to output data repeated? • 20 times, i = 0, length of array • what operations are repeated inside the loop to output data? • output next array location 15 Sorting
//exam result processing using selection sort import java.util.*; public class ExamResults { public static void main (String [] args) { int [] arr = new int[20]; input(arr); sort(arr); output(arr); } public static void input(int[] arr) { Scanner kybd = new Scanner(System.in); System.out.println("Enter exam results: "); for (int i = 0; i < arr.length; i++) { arr[i] = kybd.nextInt(); } } ExamResults.java August 14 15 Sorting 13
public static void sort(int[] arr) { for (int pass = 1; pass < arr.length; pass++) { int largestPos = findLargest(arr, arr.length - pass); if (largestPos != arr.length - pass) { swap(arr, largestPos, arr.length - pass); } output(arr); //this is for testing purposes only } } public static int findLargest(int [] arr, int num) { int largestPos = 0; for (int i = 1; i <= num; i++) { if (arr[i] > arr[largestPos]) { largestPos = i; } } return largestPos; } August 14 15 Sorting 14
public static void swap(int [] arr, int first, int second) { int temp = arr[first]; arr[first] = arr[second]; arr[second] = temp; } public static void output(int [] arr) { for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); } System.out.println(); } } August 14 15 Sorting 15
Summary In this session we have: • developed the selection sort • used the selection sort in an application In the next session we will: • searching arrays August 14 15 Sorting 16