SlideShare a Scribd company logo
CSE 373
Data Structures and Algorithms
Lecture 7: Sorting
Why Sorting?
2
 Practical application
 People by last name
 Countries by population
 Search engine results by relevance
 Fundamental to other algorithms
 Different algorithms have different asymptotic and constant-
factor trade-offs
 No single ‘best’ sort for all scenarios
 Knowing one way to sort just isn’t enough
 Many to approaches to sorting which can be used for other
problems
Problem statement
3
 There are n comparable elements in an array and we want to
rearrange them to be in increasing order
 Pre:
 An array A of data records
 A value in each data record
 A comparison function
 <, =, >, compareTo
 Post:
 For each distinct position i and j of A, if i<j then A[i]  A[j]
 A has all the same data it started with
Sorting Classification
4
In memory sorting
External
sorting
Comparison sorting
(N log N)
Specialized
Sorting
O(N2
) O(N log N) O(N)
# of tape
accesses
• Bubble Sort
• Selection Sort
• Insertion Sort
• Shell Sort
• Merge Sort
• Quick Sort
• Heap Sort
• Bucket Sort
• Radix Sort
• Simple
External
Merge Sort
• Variations
Comparison Sorting
Determine order through comparisons on the input data
Bogo sort
6
 bogo sort: orders a list of values by repetitively shuffling
them and checking if they are sorted
 more specifically:
 scan the list, seeing if it is sorted
 if not, shuffle the values in the list and repeat
 This sorting algorithm has terrible performance!
 Can we deduce its runtime?
 What about best case?
Bogo sort code
7
public static void bogoSort(int[] a) {
while (!isSorted(a)) {
shuffle(a);
}
}
// Returns true if array a's elements
// are in sorted order.
public static boolean isSorted(int[] a) {
for (int i = 0; i < a.length - 1; i++) {
if (a[i] > a[i+1]) {
return false;
}
}
return true;
}
Bogo sort code, helpers
8
// Shuffles an array of ints by randomly swapping each
// element with an element ahead of it in the array.
public static void shuffle(int[] a) {
for (int i = 0; i < a.length - 1; i++) {
// pick random number in [i+1, a.length-1] inclusive
int range = (a.length – 1) - (i + 1) + 1;
int j = (int)(Math.random() * range + (i + 1));
swap(a, i, j);
}
}
// Swaps a[i] with a[j].
private static void swap(int[] a, int i, int j) {
if (i == j)
return;
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
O(n2
) Comparison Sorting
Bubble sort
10
 bubble sort: orders a list of values by repetitively
comparing neighboring elements and swapping their
positions if necessary
 more specifically:
 scan the list, exchanging adjacent elements if they are not in
relative order; this bubbles the highest value to the top
 scan the list again, bubbling up the second highest value
 repeat until all elements have been placed in their proper
order
"Bubbling" largest element
11
 Traverse a collection of elements
 Move from the front to the end
 "Bubble" the largest value to the end using pair-wise
comparisons and swapping
5
12
35
42
77 101
0 1 2 3 4 5
Swap
42 77
"Bubbling" largest element
12
 Traverse a collection of elements
 Move from the front to the end
 "Bubble" the largest value to the end using pair-wise
comparisons and swapping
5
12
35
77
42 101
0 1 2 3 4 5
Swap
35 77
"Bubbling" largest element
13
 Traverse a collection of elements
 Move from the front to the end
 "Bubble" the largest value to the end using pair-wise
comparisons and swapping
5
12
77
35
42 101
0 1 2 3 4 5
Swap
12 77
"Bubbling" largest element
14
 Traverse a collection of elements
 Move from the front to the end
 "Bubble" the largest value to the end using pair-wise
comparisons and swapping
5
77
12
35
42 101
0 1 2 3 4 5
No need to swap
"Bubbling" largest element
15
 Traverse a collection of elements
 Move from the front to the end
 "Bubble" the largest value to the end using pair-wise
comparisons and swapping
5
77
12
35
42 101
0 1 2 3 4 5
Swap
5 101
"Bubbling" largest element
16
 Traverse a collection of elements
 Move from the front to the end
 "Bubble" the largest value to the end using pair-wise
comparisons and swapping
77
12
35
42 5
0 1 2 3 4 5
101
Largest value correctly placed
Bubble sort code
17
public static void bubbleSort(int[] a) {
for (int i = 0; i < a.length; i++) {
for (int j = 1; j < a.length - i; j++) {
// swap adjacent out-of-order elements
if (a[j-1] > a[j]) {
swap(a, j-1, j);
}
}
}
}
Bubble sort runtime
18
 Running time (# comparisons) for input size n:
 number of actual swaps performed depends on the data; out-of-
order data performs many swaps
1
j =1
n −1−i
∑
i=0
n −1
∑ = (n −1− i)
i=0
n −1
∑
= n 1
i=0
n −1
∑− 1
i=0
n −1
∑− i
i=0
n −1
∑
= n2
− n −
(n −1)n
2
= Θ(n2
)
Selection sort
19
 selection sort: orders a list of values by repetitively
putting a particular value into its final position
 more specifically:
 find the smallest value in the list
 switch it with the value in the first position
 find the next smallest value in the list
 switch it with the value in the second position
 repeat until all values are in their proper places
Selection sort example
20
Index
0 1 2 3 4 5 6 7
Value
27 63 1 72 64 58 14 9
1st
pass
1 63 27 72 64 58 14 9
2nd
pass
1 9 27 72 64 58 14 63
3rd
pass
1 9 14 72 64 58 27 63
…
Selection sort example 2
21
Selection sort code
22
public static void selectionSort(int[] a) {
for (int i = 0; i < a.length; i++) {
// find index of smallest element
int minIndex = i;
for (int j = i + 1; j < a.length; j++) {
if (a[j] < a[minIndex]) {
minIndex = j;
}
}
// swap smallest element with a[i]
swap(a, i, minIndex);
}
}
Selection sort runtime
23
 Running time for input size n:
 In practice, a bit faster than bubble sort. Why?
1
j =i+1
n −1
∑
i=0
n −1
∑ = (n −1− (i +1) +1)
i=0
n −1
∑
= (n − i −1)
i=0
n −1
∑
= n 1
i=0
n −1
∑− i
i=0
n −1
∑− 1
i=0
n −1
∑
= n2
−
(n −1)n
2
− n
= Θ(n2
)
Insertion sort
24
 insertion sort: orders a list of values by repetitively
inserting a particular value into a sorted subset of the list
 more specifically:
 consider the first item to be a sorted sublist of length 1
 insert the second item into the sorted sublist, shifting the first
item if needed
 insert the third item into the sorted sublist, shifting the other
items as needed
 repeat until all values have been inserted into their proper
positions
Insertion sort
25
 Simple sorting algorithm.
 n-1 passes over the array
 At the end of pass i, the elements that occupied A[0]…A[i]
originally are still in those spots and in sorted order.
2 8 15 1 17 10 12 5
0 1 2 3 4 5 6 7
1 2 8 15 17 10 12 5
0 1 2 3 4 5 6 7
after
pass 2
after
pass 3
2 15 8 1 17 10 12 5
0 1 2 3 4 5 6 7
Insertion sort example
26
Insertion sort code
27
public static void insertionSort(int[] a) {
for (int i = 1; i < a.length; i++) {
int temp = a[i];
// slide elements down to make room for a[i]
int j = i;
while (j > 0 && a[j - 1] > temp) {
a[j] = a[j - 1];
j--;
}
a[j] = temp;
}
}
Insertion sort runtime
28
 worst case: reverse-ordered elements in array.
 best case: array is in sorted ascending order.
 average case: each element is about halfway in order.
€
i
i=1
n −1
∑=1+ 2 + 3+ ...+ (n −1) =
(n −1)n
2
= Θ(n2
)
€
1
i=1
n −1
∑= n −1 = Θ(n)
i
2
i=1
n −1
∑ =
1
2
(1+ 2 + 3...+ (n −1)) =
(n −1)n
4
= Θ(n2
)
Comparing sorts
29
 We've seen "simple" sorting algorithms so far, such as
selection sort and insertion sort.
 They all use nested loops and perform approximately n2
comparisons
 They are relatively inefficient
Sorting practice problem
30
 Consider the following array of int values.
 [22, 11, 34, -5, 3, 40, 9, 16, 6]
 (a) Write the contents of the array after 3 passes of the
outermost loop of bubble sort.
 (b) Write the contents of the array after 5 passes of the
outermost loop of insertion sort.
 (c) Write the contents of the array after 4 passes of the
outermost loop of selection sort.

More Related Content

PPT
Decimal Long Double Double Double. Represents double-precision floating-point...
Anwar Patel
 
PPT
14-sorting (3).ppt
yasser3omr
 
PPT
14-sorting.ppt
KamalAlbashiri
 
PPT
14-sorting.ppt
RenalthaPujaBagaskar
 
PPT
14-sorting.ppt
SushantRaj25
 
PPTX
sorting.pptx
DrRanjeetKumar51721
 
PPTX
Introduction to Algorithms
pppepito86
 
PPTX
Searching and sorting Techniques in Data structures
PRIANKA R
 
Decimal Long Double Double Double. Represents double-precision floating-point...
Anwar Patel
 
14-sorting (3).ppt
yasser3omr
 
14-sorting.ppt
KamalAlbashiri
 
14-sorting.ppt
RenalthaPujaBagaskar
 
14-sorting.ppt
SushantRaj25
 
sorting.pptx
DrRanjeetKumar51721
 
Introduction to Algorithms
pppepito86
 
Searching and sorting Techniques in Data structures
PRIANKA R
 

Similar to 07-sorting.ppt for insertion sort and bubble sorting technquies (20)

PPT
search_sort Search sortSearch sortSearch sortSearch sort
Shanmuganathan C
 
PDF
Sorting and Hashing Algorithm full pdfs.
NikhilSoni177492
 
PPT
search_sort search_sortsearch_sort search_sortsearch_sortsearch_sortsearch_sort
Kanupriya731200
 
PPTX
Different Searching and Sorting Methods.pptx
Minakshee Patil
 
PPT
search_sort_v1.pptgghghhhggggjjjjjjllllllllvbbbbbcfdsdfffg
arpitasahad87
 
PPTX
LectureSlidData_sturcture_algorithm_v2.pptx
abhaysharma999437
 
PPT
Chapter 11 - Sorting and Searching
Eduardo Bergavera
 
PDF
Sorting
Zaid Shabbir
 
PDF
Sorting
Zaid Shabbir
 
PPT
16-sorting.ppt
18Gunaalanpg
 
PPT
Sorting
Shaista Qadir
 
PDF
Bin Sorting And Bubble Sort By Luisito G. Trinidad
LUISITO TRINIDAD
 
PPT
Insersion & Bubble Sort in Algoritm
Ehsan Ehrari
 
PPT
quicksortnmsd cmz ,z m,zmm,mbfjjjjhjhfjsg
RafishaikIT02044
 
PPT
03_sorting123456789454545454545444543.ppt
ssuser7b9bda1
 
PPT
03_sorting and it's types with example .ppt
vanshii9976
 
PPTX
Algorithms and Data Structures - Parahyangan Catholic University Credit Lionov
Pratik Parmar
 
PPTX
searching in data structure.pptx
chouguleamruta24
 
PPT
Sorting Algorithms.
Saket Kumar
 
PPTX
Algorithms - "Chapter 2 getting started"
Ra'Fat Al-Msie'deen
 
search_sort Search sortSearch sortSearch sortSearch sort
Shanmuganathan C
 
Sorting and Hashing Algorithm full pdfs.
NikhilSoni177492
 
search_sort search_sortsearch_sort search_sortsearch_sortsearch_sortsearch_sort
Kanupriya731200
 
Different Searching and Sorting Methods.pptx
Minakshee Patil
 
search_sort_v1.pptgghghhhggggjjjjjjllllllllvbbbbbcfdsdfffg
arpitasahad87
 
LectureSlidData_sturcture_algorithm_v2.pptx
abhaysharma999437
 
Chapter 11 - Sorting and Searching
Eduardo Bergavera
 
Sorting
Zaid Shabbir
 
Sorting
Zaid Shabbir
 
16-sorting.ppt
18Gunaalanpg
 
Sorting
Shaista Qadir
 
Bin Sorting And Bubble Sort By Luisito G. Trinidad
LUISITO TRINIDAD
 
Insersion & Bubble Sort in Algoritm
Ehsan Ehrari
 
quicksortnmsd cmz ,z m,zmm,mbfjjjjhjhfjsg
RafishaikIT02044
 
03_sorting123456789454545454545444543.ppt
ssuser7b9bda1
 
03_sorting and it's types with example .ppt
vanshii9976
 
Algorithms and Data Structures - Parahyangan Catholic University Credit Lionov
Pratik Parmar
 
searching in data structure.pptx
chouguleamruta24
 
Sorting Algorithms.
Saket Kumar
 
Algorithms - "Chapter 2 getting started"
Ra'Fat Al-Msie'deen
 
Ad

Recently uploaded (20)

PPTX
Unit 5 BSP.pptxytrrftyyydfyujfttyczcgvcd
ghousebhasha2007
 
PPTX
Module2 Data Base Design- ER and NF.pptx
gomathisankariv2
 
PPTX
Victory Precisions_Supplier Profile.pptx
victoryprecisions199
 
PPTX
Production of bioplastic from fruit peels.pptx
alwingeorgealwingeor
 
PDF
flutter Launcher Icons, Splash Screens & Fonts
Ahmed Mohamed
 
PDF
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
PPTX
MSME 4.0 Template idea hackathon pdf to understand
alaudeenaarish
 
PDF
Software Testing Tools - names and explanation
shruti533256
 
PDF
Principles of Food Science and Nutritions
Dr. Yogesh Kumar Kosariya
 
PDF
July 2025: Top 10 Read Articles Advanced Information Technology
ijait
 
PDF
Top 10 read articles In Managing Information Technology.pdf
IJMIT JOURNAL
 
PPTX
Civil Engineering Practices_BY Sh.JP Mishra 23.09.pptx
bineetmishra1990
 
PDF
LEAP-1B presedntation xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
hatem173148
 
PDF
2010_Book_EnvironmentalBioengineering (1).pdf
EmilianoRodriguezTll
 
PPTX
TE-AI-Unit VI notes using planning model
swatigaikwad6389
 
PPTX
AgentX UiPath Community Webinar series - Delhi
RohitRadhakrishnan8
 
PPTX
Chapter_Seven_Construction_Reliability_Elective_III_Msc CM
SubashKumarBhattarai
 
PDF
Introduction to Data Science: data science process
ShivarkarSandip
 
PDF
Chad Ayach - A Versatile Aerospace Professional
Chad Ayach
 
PPT
SCOPE_~1- technology of green house and poyhouse
bala464780
 
Unit 5 BSP.pptxytrrftyyydfyujfttyczcgvcd
ghousebhasha2007
 
Module2 Data Base Design- ER and NF.pptx
gomathisankariv2
 
Victory Precisions_Supplier Profile.pptx
victoryprecisions199
 
Production of bioplastic from fruit peels.pptx
alwingeorgealwingeor
 
flutter Launcher Icons, Splash Screens & Fonts
Ahmed Mohamed
 
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
MSME 4.0 Template idea hackathon pdf to understand
alaudeenaarish
 
Software Testing Tools - names and explanation
shruti533256
 
Principles of Food Science and Nutritions
Dr. Yogesh Kumar Kosariya
 
July 2025: Top 10 Read Articles Advanced Information Technology
ijait
 
Top 10 read articles In Managing Information Technology.pdf
IJMIT JOURNAL
 
Civil Engineering Practices_BY Sh.JP Mishra 23.09.pptx
bineetmishra1990
 
LEAP-1B presedntation xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
hatem173148
 
2010_Book_EnvironmentalBioengineering (1).pdf
EmilianoRodriguezTll
 
TE-AI-Unit VI notes using planning model
swatigaikwad6389
 
AgentX UiPath Community Webinar series - Delhi
RohitRadhakrishnan8
 
Chapter_Seven_Construction_Reliability_Elective_III_Msc CM
SubashKumarBhattarai
 
Introduction to Data Science: data science process
ShivarkarSandip
 
Chad Ayach - A Versatile Aerospace Professional
Chad Ayach
 
SCOPE_~1- technology of green house and poyhouse
bala464780
 
Ad

07-sorting.ppt for insertion sort and bubble sorting technquies

  • 1. CSE 373 Data Structures and Algorithms Lecture 7: Sorting
  • 2. Why Sorting? 2  Practical application  People by last name  Countries by population  Search engine results by relevance  Fundamental to other algorithms  Different algorithms have different asymptotic and constant- factor trade-offs  No single ‘best’ sort for all scenarios  Knowing one way to sort just isn’t enough  Many to approaches to sorting which can be used for other problems
  • 3. Problem statement 3  There are n comparable elements in an array and we want to rearrange them to be in increasing order  Pre:  An array A of data records  A value in each data record  A comparison function  <, =, >, compareTo  Post:  For each distinct position i and j of A, if i<j then A[i]  A[j]  A has all the same data it started with
  • 4. Sorting Classification 4 In memory sorting External sorting Comparison sorting (N log N) Specialized Sorting O(N2 ) O(N log N) O(N) # of tape accesses • Bubble Sort • Selection Sort • Insertion Sort • Shell Sort • Merge Sort • Quick Sort • Heap Sort • Bucket Sort • Radix Sort • Simple External Merge Sort • Variations
  • 5. Comparison Sorting Determine order through comparisons on the input data
  • 6. Bogo sort 6  bogo sort: orders a list of values by repetitively shuffling them and checking if they are sorted  more specifically:  scan the list, seeing if it is sorted  if not, shuffle the values in the list and repeat  This sorting algorithm has terrible performance!  Can we deduce its runtime?  What about best case?
  • 7. Bogo sort code 7 public static void bogoSort(int[] a) { while (!isSorted(a)) { shuffle(a); } } // Returns true if array a's elements // are in sorted order. public static boolean isSorted(int[] a) { for (int i = 0; i < a.length - 1; i++) { if (a[i] > a[i+1]) { return false; } } return true; }
  • 8. Bogo sort code, helpers 8 // Shuffles an array of ints by randomly swapping each // element with an element ahead of it in the array. public static void shuffle(int[] a) { for (int i = 0; i < a.length - 1; i++) { // pick random number in [i+1, a.length-1] inclusive int range = (a.length – 1) - (i + 1) + 1; int j = (int)(Math.random() * range + (i + 1)); swap(a, i, j); } } // Swaps a[i] with a[j]. private static void swap(int[] a, int i, int j) { if (i == j) return; int temp = a[i]; a[i] = a[j]; a[j] = temp; }
  • 10. Bubble sort 10  bubble sort: orders a list of values by repetitively comparing neighboring elements and swapping their positions if necessary  more specifically:  scan the list, exchanging adjacent elements if they are not in relative order; this bubbles the highest value to the top  scan the list again, bubbling up the second highest value  repeat until all elements have been placed in their proper order
  • 11. "Bubbling" largest element 11  Traverse a collection of elements  Move from the front to the end  "Bubble" the largest value to the end using pair-wise comparisons and swapping 5 12 35 42 77 101 0 1 2 3 4 5 Swap 42 77
  • 12. "Bubbling" largest element 12  Traverse a collection of elements  Move from the front to the end  "Bubble" the largest value to the end using pair-wise comparisons and swapping 5 12 35 77 42 101 0 1 2 3 4 5 Swap 35 77
  • 13. "Bubbling" largest element 13  Traverse a collection of elements  Move from the front to the end  "Bubble" the largest value to the end using pair-wise comparisons and swapping 5 12 77 35 42 101 0 1 2 3 4 5 Swap 12 77
  • 14. "Bubbling" largest element 14  Traverse a collection of elements  Move from the front to the end  "Bubble" the largest value to the end using pair-wise comparisons and swapping 5 77 12 35 42 101 0 1 2 3 4 5 No need to swap
  • 15. "Bubbling" largest element 15  Traverse a collection of elements  Move from the front to the end  "Bubble" the largest value to the end using pair-wise comparisons and swapping 5 77 12 35 42 101 0 1 2 3 4 5 Swap 5 101
  • 16. "Bubbling" largest element 16  Traverse a collection of elements  Move from the front to the end  "Bubble" the largest value to the end using pair-wise comparisons and swapping 77 12 35 42 5 0 1 2 3 4 5 101 Largest value correctly placed
  • 17. Bubble sort code 17 public static void bubbleSort(int[] a) { for (int i = 0; i < a.length; i++) { for (int j = 1; j < a.length - i; j++) { // swap adjacent out-of-order elements if (a[j-1] > a[j]) { swap(a, j-1, j); } } } }
  • 18. Bubble sort runtime 18  Running time (# comparisons) for input size n:  number of actual swaps performed depends on the data; out-of- order data performs many swaps 1 j =1 n −1−i ∑ i=0 n −1 ∑ = (n −1− i) i=0 n −1 ∑ = n 1 i=0 n −1 ∑− 1 i=0 n −1 ∑− i i=0 n −1 ∑ = n2 − n − (n −1)n 2 = Θ(n2 )
  • 19. Selection sort 19  selection sort: orders a list of values by repetitively putting a particular value into its final position  more specifically:  find the smallest value in the list  switch it with the value in the first position  find the next smallest value in the list  switch it with the value in the second position  repeat until all values are in their proper places
  • 21. Index 0 1 2 3 4 5 6 7 Value 27 63 1 72 64 58 14 9 1st pass 1 63 27 72 64 58 14 9 2nd pass 1 9 27 72 64 58 14 63 3rd pass 1 9 14 72 64 58 27 63 … Selection sort example 2 21
  • 22. Selection sort code 22 public static void selectionSort(int[] a) { for (int i = 0; i < a.length; i++) { // find index of smallest element int minIndex = i; for (int j = i + 1; j < a.length; j++) { if (a[j] < a[minIndex]) { minIndex = j; } } // swap smallest element with a[i] swap(a, i, minIndex); } }
  • 23. Selection sort runtime 23  Running time for input size n:  In practice, a bit faster than bubble sort. Why? 1 j =i+1 n −1 ∑ i=0 n −1 ∑ = (n −1− (i +1) +1) i=0 n −1 ∑ = (n − i −1) i=0 n −1 ∑ = n 1 i=0 n −1 ∑− i i=0 n −1 ∑− 1 i=0 n −1 ∑ = n2 − (n −1)n 2 − n = Θ(n2 )
  • 24. Insertion sort 24  insertion sort: orders a list of values by repetitively inserting a particular value into a sorted subset of the list  more specifically:  consider the first item to be a sorted sublist of length 1  insert the second item into the sorted sublist, shifting the first item if needed  insert the third item into the sorted sublist, shifting the other items as needed  repeat until all values have been inserted into their proper positions
  • 25. Insertion sort 25  Simple sorting algorithm.  n-1 passes over the array  At the end of pass i, the elements that occupied A[0]…A[i] originally are still in those spots and in sorted order. 2 8 15 1 17 10 12 5 0 1 2 3 4 5 6 7 1 2 8 15 17 10 12 5 0 1 2 3 4 5 6 7 after pass 2 after pass 3 2 15 8 1 17 10 12 5 0 1 2 3 4 5 6 7
  • 27. Insertion sort code 27 public static void insertionSort(int[] a) { for (int i = 1; i < a.length; i++) { int temp = a[i]; // slide elements down to make room for a[i] int j = i; while (j > 0 && a[j - 1] > temp) { a[j] = a[j - 1]; j--; } a[j] = temp; } }
  • 28. Insertion sort runtime 28  worst case: reverse-ordered elements in array.  best case: array is in sorted ascending order.  average case: each element is about halfway in order. € i i=1 n −1 ∑=1+ 2 + 3+ ...+ (n −1) = (n −1)n 2 = Θ(n2 ) € 1 i=1 n −1 ∑= n −1 = Θ(n) i 2 i=1 n −1 ∑ = 1 2 (1+ 2 + 3...+ (n −1)) = (n −1)n 4 = Θ(n2 )
  • 29. Comparing sorts 29  We've seen "simple" sorting algorithms so far, such as selection sort and insertion sort.  They all use nested loops and perform approximately n2 comparisons  They are relatively inefficient
  • 30. Sorting practice problem 30  Consider the following array of int values.  [22, 11, 34, -5, 3, 40, 9, 16, 6]  (a) Write the contents of the array after 3 passes of the outermost loop of bubble sort.  (b) Write the contents of the array after 5 passes of the outermost loop of insertion sort.  (c) Write the contents of the array after 4 passes of the outermost loop of selection sort.

Editor's Notes

  • #21: The selection sort marks the first element (27). It then goes through the remaining data to find the smallest number(1). It swaps this with the first element and the smallest element is now in its correct position. It then marks the second element (63) and looks through the remaining data for the next smallest number (9). These two numbers are then swapped. This process continues until n-1 passes have been made.
  • #23: faster because fewer swaps are made. (Constant c is smaller on O(n^2).)