L9_Sorting

# L9_Sorting - Sorting In this lecture (and the next) we...

This preview shows pages 1–12. Sign up to view the full content.

Sorting In this lecture (and the next) we shall again take a detour from data structures, and investigate different sorting algorithms and analyse their efficiency. Page 1 of 39

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Sorting SORT 8 7 6 4 3 3 6 8 4 7 Sorting an integer array into ascending order Page 2 of 39
Sorting SORT 'a' 'm' 'n' 'p' 's' 'p' 's' 'a' 'n' 'm' Sorting a character array into descending order Page 3 of 39

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Sorting SORT sea moon kite eye an moon an eye kite sea Sorting a word array into ‘dictionary’ order Page 4 of 39
Sorting Algorithms There are many different sorting algorithms. We shall introduce the most common ones. In this lecture, we shall first discuss one simple sorting algorithm, the Selection Sort , and analyse its perform- ance. Page 5 of 39

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Some sorting algorithms are faster, while some are slower. Faster algo- rithms are said to be more efficient , while slower ones are said to be less efficient. Page 6 of 39
The Selection Sort Algorithm Page 7 of 39

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
The Selection Sort Algorithm 5 2 8 7 Example: to sort into ascending order. 2 8 7 5 7 8 2 Step 1: Determine position 1 5 2 5 7 8 2 5 7 Step 3: Determine position 3 8 2 5 7 8 2 8 7 5 Step 2: Determine position 2 8 7 5 2 Step 4: Determine position 4 Page 8 of 39
Now, let’s write the selection sort function together! void selectionSort(int[] A) { } A is the integer array to be sorted, and n is the length of the array. Page 9 of 39

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
void selectionSort(int[] A) { int n = A.length; for (int position = 0, 1, 2, …, n – 1) { 1. Select the smallest integer from between position to n – 1 . 2. Swap the selected integer and the integer at position . position 7 8 2 5 2 8 7 5 2 5 7 8 8 7 5 2 } } Page 10 of 39
void selectionSort(int[] A) { int n = A.length; for (int position = 0; position < n; position++) { 1. Select the smallest integer from between posi- tion to n – 1 .

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 12/28/2010 for the course CSC CSC1110 taught by Professor Cjyuan during the Fall '06 term at CUHK.

### Page1 / 39

L9_Sorting - Sorting In this lecture (and the next) we...

This preview shows document pages 1 - 12. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online