Title: Programming
1Programming
2Sorting
- To arrange a set of items in sequence.
- It was estimated that 2550 of all computing
power is being expended in sorting activities. - Possible reasons
- Many applications require sorting
- Many applications perform sorting when they don't
have to - Many applications use inefficient sorting
algorithms
3Sorting Applications
- List of student ID names and numbers in a table
(sorted by name) - List of scores before letter grade
- assignment (sorted by students' scores)
- List of horses after a race (sorted by the
finishing times) - To prepare an originally unsorted array for
ordered binary searching
4Some Sorting Methods
- Selection sort
- Bubble sort
- Shell sort
- Quick sort
5Selection Sort
- Selection sort performs sorting by
- repeatedly putting the smallest element in the
first position of the unsorted portion, - until the whole array is sorted.
- It is similar to the way that many people do
their sorting.
6Selection Sort
- Algorithm
- 1. Define the unsorted portion of the array as
the entire array - 2. While the unsorted portion of the array has
more than one element ? Find its smallest
element - ? Swap with first element of the
sorted portion of the array - ? Reduce unsorted portion of the array by 1
7Selection Sort
http//www.cs.brockport.edu/cs/java/apps/sorters/s
elsort.html
One pass to find the smallest number Swap with
List0.
min_index 0 for (j 1 j lt n j) if
(Listj lt Listmin_index) min_index j
swap(List0, Listmin_index)
8Selection Sort
http//www.cs.brockport.edu/cs/java/apps/sorters/s
elsort.html
N passes to find the current smallest number
swap with Listi.
for (i 0 i lt (n - 1) i) min_index i
for (j i 1 j lt n j) if (Listj lt
Listmin_index) min_index j
swap(Listi, Listmin_index)
9Selection Sort
- include ltiostreamgt
- using namespace std
- void select(int data, int size)
- void main()
- int A9 10, 7, 9, 1, 9, 17, 30, 5, 6
- select(A, 9)
- cout ltlt "The sorted array "
- for(int i0 ilt9 i)
- cout ltlt Ai ltlt " "
- cout ltlt endl
-
10Selection Sort
- // Sort array of integers in ascending order
- void select(int data, // in/output array
- int size) // input array size
- int temp // for swap
- int min_index // index of min value
- for(int leftmost0 leftmostltsize leftmost)
- // find the smallest item
- min_index leftmost
- for(int ileftmost iltsize i)
- if (datai lt datamin_index)
- min_index i
- // swap with last item if necessary
- if(datamin_index lt dataleftmost)
- temp datamin_index /// swap
- datamin_index dataleftmost
- dataleftmost temp
-
-
11Bubble Sort
- Bubble sort examines the array from start to
finish, comparing elements as it goes. - Any time it finds a larger element before a
smaller element, it swaps the two. - In this way, the larger elements are passed
towards the end. - The largest element of the array therefore
"bubbles" to the end of the array. - It then repeats the process for the unsorted
portion of the array until the whole array is
sorted.
12Bubble Sort
- Bubble sort works on the same general principle
as shaking a soft drink bottle. - Right after shaking, the contents are a mixture
of bubbles and soft drink, distributed randomly. - Because bubbles are lighter than the soft drink,
they rise to the surface, displacing the soft
drink downwards. - This is how bubble sort got its name, because the
smaller elements "float" to the top, while the
larger elements "sink" to the bottom.
13Bubble Sort
- Algorithm
- Define the unsorted portion of the array to be
the entire array - While the unsorted portion of the array has more
than one element - 1. For every element in the unsorted portion,
swap with the next neighbor if it is larger than
the neighbor - 2. Reduce the unprocessed portion of the array
by 1
14Bubble Sort
http//www.cs.brockport.edu/cs/java/apps/sorters/b
ubblesort.html
List0, List1, Listn-2, Listn-1
One pass to sink the largest number to the
bottom of the list
for (j0 jltn-1 j) if (Listj gt
Listj1) swap(Listj, Listj1)
15Bubble Sort
http//www.cs.brockport.edu/cs/java/apps/sorters/b
ubblesort.html
N passes to sink the currently largest number
for (in igt1 i--) for (j0 jlti-1 j) if
(Listj gt Listj1) swap(Listj,
Listj1)
16Bubble Sort
- // Sort an array of integers in ascending order
- void bubble(int data, // in/output array
- int size) // input array size
- int temp // for swap
- for(int rightmostsize-1 rightmostgt0
rightmost--) - for(int i0 iltrightmost i)
- if(datai gt datai1 )
- // swap with next item if greater
- temp datai
- datai datai1
- datai1 temp
-
-
-