This preview shows pages 1–7. Sign up to view the full content.
Programming
Sorting
Arrays
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document P102 Prog. Fundamentals. Sorting I/ Slide 2
Sorting
●
To arrange a set of items in sequence.
●
It is estimated that 25~50% of all
computing power is used for sorting
activities.
●
Possible reasons:
■
Many applications require sorting;
■
Many applications perform sorting when they
don't have to;
■
Many applications use inefficient sorting
algorithms.
P102 Prog. Fundamentals. Sorting I/ Slide 3
Sorting Applications
●
To prepare a list of student ID, names, and
scores in a table (sorted by ID or name) for easy
checking.
●
To prepare a list of scores before letter grade
assignment.
●
To produce a list of horses after a race (sorted
by the finishing times) for payoff calculation.
●
To prepare an originally unsorted array for
ordered binary searching.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document P102 Prog. Fundamentals. Sorting I/ Slide 4
Some Sorting Methods
●
Selection sort
●
Bubble sort
●
Shell sort (a simple but faster sorting
method than above; see p.331 of
Numerical Recipes in C
, 2nd ed., by
William H. Press
et al
, Cambridge
University Press, 1992)
●
Quick sort (a very efficient sorting method
for most applications; p.332336,
ibid
.)
P102 Prog. Fundamentals. Sorting I/ Slide 5
Ex. 1A:
Selection Sort
●
Selection sort performs sorting by
repeatedly putting the largest element in
the unsorted portion of the array to the
end of this unsorted portion until the whole
array is sorted.
●
It is similar to the way that many people do
their sorting.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Ex. 1A: Selection Sort
●
Algorithm
1. Define the entire array as the unsorted
portion of the array
2. While the unsorted portion of the array
has more than one element:
⇒
Find its largest element.
⇒
Swap with last element (assuming their
This is the end of the preview. Sign up
to
access the rest of the document.
This note was uploaded on 08/07/2011 for the course COMP 102 taught by Professor Daniel during the Spring '08 term at HKUST.
 Spring '08
 Daniel
 Sort

Click to edit the document details