Download presentation
Presentation is loading. Please wait.
1
15 Sorting1June 151 15 Sorting CE00858-1: Fundamental Programming Techniques
2
15 Sorting2June 15 Objectives In this session, we will develop a sorting algorithm use this sort in a problem June 152
3
15 Sorting3June 15 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 June 153
4
15 Sorting 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 June 154
5
15 Sorting Selection sort demonstration 9 8 6 2 5 5 8 6 2 9 5 2 6 8 9 2 5 6 8 9 pass 1: check data in locations 0 to 4 9 in location 0 is largest swap with element in location 4 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 June 155
6
15 Sorting6June 15 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 June 156
7
15 Sorting7June 15 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 } June 157
8
15 Sorting8June 15 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; } June 158
9
15 Sorting9June 15 Swap method public static void swap(int [] arr, int first, int second) { int temp = arr[first]; arr[first] = arr[second]; arr[second] = temp; } June 159
10
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 Sorting10June 15
11
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 Sorting11June 15
12
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 Sorting12June 15
13
15 Sorting13June 15 //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(); } June 1513 ExamResults.java
14
15 Sorting14June 15 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; } June 1514
15
15 Sorting15June 15 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(); } June 1515
16
15 Sorting16June 1516June 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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.