Title: Programming
1Programming
2Sorting
- To arrange a set of items in sequence.
- It is estimated that 2550 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.
3Sorting 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.
4Some 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.332-336, ibid.)
5Ex. 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.
6Ex. 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
values are different). - ? Reduce the size of the unsorted portion
of the array by 1.
7Before sorting 14 2 10 5 1 3 17 7
After pass 1 14 2 10 5 1 3 7 17
After pass 2 7 2 10 5 1 3 14 17
After pass 3 7 2 3 5 1 10 14 17
After pass 4 1 2 3 5 7 10 14 17
8- // Sort array of integers in ascending order
- void select(int data, // in/output array
- int size) // input array size
- int temp // for swap
- int max_index // index of max value
- for (int rightmostsize-1 rightmostgt0
rightmost--) - //find the largest item in the unsorted portion
- //rightmost is the end point of the unsorted
part of array - max_index 0 //points the largest element
- for ( int current1 currentltrightmost
current) - if (datacurrent gt datamax_index)
- max_index current
- //swap the largest item with last item if
necessary - if (datamax_index gt datarightmost)
- temp datamax_index // swap
- datamax_index datarightmost
- datarightmost temp
-
-
9- const int array_size 8
- int main()
- int listarray_size 14, 2, 10, 5, 1,
- 3, 17, 7
- int index, ans
- cout ltlt "Before sorting "
- for (index 0 indexltarray_size index)
- cout ltlt listindex ltlt" "
- cout ltlt endl
- cout ltlt "Enter sorting method 1(select),
2(bubble)" - cin gtgt ans
- if (ans 1) select(list, array_size)
- else bubble(list, array_size)
- cout ltlt endl ltlt "After sorting "
- for (index 0 indexltarray_size index)
- cout ltlt listindex ltlt" "
- cout ltlt endl
- return 0
10Ex. 1B Bubble 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. - Then it repeats the process for the unsorted
portion of the array until the whole array is
sorted.
11Ex. 1A Bubble 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.
12Ex. 1B Bubble Sort
- Algorithm
- Define the entire array as the unsorted portion
of the 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 size of the unsorted portion of
the array by 1.
13Before sorting 14 2 10 5 1 3 17 7
outer7, inner0 2 14 10 5 1 3 17 7
outer7, inner1 2 10 14 5 1 3 17 7
outer7, inner2 2 10 5 14 1 3 17 7
outer7, inner3 2 10 5 1 14 3 17 7
outer7, inner4 2 10 5 1 3 14 17 7
outer7, inner6 2 10 5 1 3 14 7 17
outer6, inner1 2 5 10 1 3 14 7 17
outer6, inner2 2 5 1 10 3 14 7 17
outer6, inner3 2 5 1 3 10 14 7 17
outer6, inner5 2 5 1 3 10 7 14 17
outer5, inner1 2 1 5 3 10 7 14 17
outer5, inner2 2 1 3 5 10 7 14 17
outer5, inner4 2 1 3 5 7 10 14 17
outer4, inner0 1 2 3 5 7 10 14 17
---
outer1, inner0 1 2 3 5 7 10 14 17
14- //Example1b Bobble 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 outersize-1 outer gt 0 outer--)
- for (int inner0 inner lt outer inner)
- // traverse the nested loops
- if ( datainner gt datainner1 )
- // swap current element with next
- // if the current element is greater
- temp datainner
- datainner datainner1
- datainner1 temp
-
- // inner for loop
- // outer for loop
15- const int array_size 12, string_size 11
- int main()
- char montharray_sizestring_size
"January", - "February","March","April","May","June","July","A
ugust", - "September", "October", "November", "December"
- int index, ans
-
- cout ltlt "Enter sorting method 1(select),
2(bubble) " - cin gtgt ans
- if (ans 1) select(month, array_size)
- else bubble(month, array_size)
- cout ltlt endl ltlt "After sorting "
- for (index 0 indexltarray_size index)
- cout ltlt monthindex ltlt" "
- cout ltlt endl
- return 0
160 1 2 3 4 5 6 7 8 9 10
month0 J a n u a r y \0
month1 F e b r u a r y \0
month2 M a r c h \0
month3 A p r i l \0
month4 M a y \0
month5 J u n e \0
month6 J u l y \0
month7 A u g u s t \0
month8 S e p t e m b e r \0
month9 O c t c b e r \0
month10 N o v e m b e r \0
month11 D e c e m b e r \0
17- // Sort array of strings in ascending order
- void select(char datastring_size, //
in/output array - int size) // input array size
- char tempstring_size // for swap
- int max_index // index of max value
- for (int rightmostsize-1 rightmostgt0
rightmost--) - // find the largest item
- max_index 0
- for(int current1 currentltrightmost
current) - if (strcmp(datacurrent,
datamax_index) gt0) - max_index current
- // swap with last item if necessary
- if(strcmp(datamax_index, datarightmost)gt0)
- strcpy(temp,datamax_index) // swap
- strcpy(datamax_index,datarightmost)
- strcpy(datarightmost, temp)
- for (int index0 indexlt size index)
- cout ltlt dataindex ltlt " "
- coutltltendl
18- // Sort an array of strings in ascending order
- void bubble(char datastring_size, //
in/outputarray - int size) // input array
size - char tempstring_size // for swap
- for(int outersize-1 outergt0 outer--)
- for (int inner0 inner lt outer inner)
- // traverse the nested loops
- if ( strcmp(datainner, datainner1)gt0 )
-
- // swap current element with next
- // if the current element is
greater - strcpy(temp, datainner)
- strcpy(datainner,
datainner1) - strcpy(datainner1, temp)
- for (int index0 indexlt size
index) - cout ltlt dataindex ltlt " "
- coutltltendl
-
- // inner for loop