Algortihms
and Data
Structures:
Basic Sorting
DEPARTMENT OF INFORMATICS
PARAHYANGAN CATHOLIC UNIVERSITY
CREDIT TO: LIONOV, M.SC
Definition
Sorting is the process of rearranging a sequence of objects and put
them in some logical order.
For this problem to be meaningful, the nature of the list items must
allow such an ordering.
Computational problem :
◦ Input: A sequence of n numbers
◦ Output: A permutation (reordering) of the input sequence such that
Definition (2)
Example : Given the input sequence {31, 41, 59, 26, 41, 58}, a sorting
algorithm returns as output the sequence {26, 31, 41, 41, 58, 59}
In practice, not only numbers: characters, strings, collection of data
(records) containing a key to be sorted or any other ordered types.
Motivation
Sorting is the most fundamental algorithmic problem in computer
science:
◦ Sorting is a starting point to solve other problems and basic building block for
many other algorithms (ex: Greedy, Searching)
◦ Design of algorithms similar in sorting are effective in addressing other
problems, ex: divide-and-conquer
Motivation (2)
◦ Historically, computers spent more time to sort than doing anything else. If
that fraction is lower today, one likely reason is that sorting algorithms are
relatively efficient, not that sorting has diminished in relative importance.
◦ Literally dozens of different algorithms are known, most of which possess
some particular advantage over all other algorithms in certain situations.
◦ Sorting plays a major role in commercial data processing and in modern
scientific computing.
Characteristics
Stable:
◦ Preserves the relative order of any two equal elements in its input. If an input list
contains two equal elements in positions i and j where i < j, then in the sorted
list they have to be in positions i’ and j’ respectively, such that i’ < j’.
In-place:
◦ An algorithm is said to be in-place if it does not require extra memory, except,
possibly, for a few memory units.
Characteristics (2)
Internal-External:
◦ Whether there are so many records to be sorted that they must be kept in
external files (on disks, tapes, etc) or whether they can all be kept internally
in high-speed memory
Comparison-based:
◦ Comparison-based sorting works by comparing elements to be sorted.
◦ Non-comparison based sorting: radix sort, bucket sort, count sort, etc.
Types of Sorting Algorithms
Sorting algorithms can be divided based on its design
◦ Exhaustive Search: permutation based sorting
◦ Elementary method :
◦ Brute Force: Selection Sort
◦ Decrease-and-Conquer : Insertion Sort
◦ Uncategorized : Shell Sort, Bubble Sort
Types of Sorting Algorithms (2)
◦ Divide-and-Conquer : Quick Sort, Merge Sort
◦ Data Structure/Transform-and-Conquer : Heap Sort
◦ Non comparison-based: Count Sort, Radix Sort
◦ Sorting network : Bitonic Sort, Odd-Even Transposition Sort
Usually, Bubble Sort is the first sorting algorithm to be taught to the
student. However, because it extremely easy to remember and then
students tend to use it in every situation, we opt not to teach it in the
class, look up by yourself over the internet
Pragmatics of Sorting
In what order do the items should be sorted ?
◦ Increasing or decreasing order ?
◦ Ascending order: for all
◦ Descending order: for all
◦ Sorting just the key or an entire record?
Sorting a data set involves maintaining the integrity of complex data records. Example: A
mailing list of names, addresses, and phone numbers may be sorted by names as the key
field, but it had better retain the linkage between names and addresses.
Pragmatics of Sorting (2)
In what order do the items should be sorted ?
◦ Equal keys?
Sometimes the relative order among elements with equal key values is important. Either
they have a secondary key or enforce the relative order among these equal keys (stable).
◦ Non-numerical data ?
The right way to specify such matters in the sorting algorithm is with an application-
specific pairwise-element comparison function.
Running Time
Running time for Sorting Algorithms usually depend on the number of
◦ comparisons (for comparison-based algorithm)
◦ data movements or exchanges (or array access).
Running Time (2)
The number of comparisons and the number of movements do not have to
coincide. Practical reasons must aid in the choice of which algorithm to
use:
◦ comparisons become more important if strings or arrays of numbers are
compared
◦ exchanges become more important if the data items are large (such as
structure, record)
Running Time
Overall, efficiency is measured for the following cases:
◦ Best case : data already in order
◦ Worst case : data in reverse order
◦ Average case : data in random order
Selection Sort-Idea
Selection sort works by selecting an element and move it to its proper
position :
◦ Assume we have an array of n integers with random order
◦ Find the smallest elements in the array
◦ Exchange the smallest elements with the first element in the array
◦ Starting from the second element, find the smallest element among the last
n-1 elements
◦ Exchange it with the second element in the array
◦ Continue until only one element remain unsorted
Selection Sort-Illustration
21 40 5 62 -123 15
-123 40 5 62 21 15
-123 5 40 62 21 15
-123 5 15 62 21 40
-123 5 15 21 62 40
-123 5 15 21 40 62
Selection Sort-Pseudocode
Problem : Sort an array of integers in ascending order
Input : Array A of integers, contains n integer
Output : All elements in A are sorted
1. SELECTION-SORT(A)
2. for i =1 to n-1 do
3. select the i-th smallest element
4. exchange with the i-th element
1. SELECTION-SORT(A)
2. for i =1 to n-1 do
3. min = i
4. for j = i+1 to n do
5. if A[j] < A[min]
6. min = j
7. exchange A[min]with A[i]
Selection Sort-Running Time
1. SELECTION-SORT(A)
2. for i =1 to n-1 do
3. min = i
4. for j = i+1 to n do
5. if A[j] < A[min]
6. min = j
7. exchange A[min]with A[i]
Running time of Selection Sort
◦ Best Case: Line 5 will be excecuted
◦ Average Case: Line 5 will be excecuted
◦ Worst Case: Line 5 will be excecuted
Running time of Selection Sort:
Insertion Sort-Idea
Selection sort works by inserting an element into its proper position in
already sorted (sub)-array:
◦ Assume we have an array of n integers with random order
◦ The first element in already sorted
◦ Insert the second element into already sorted sub-array contains the first
element
◦ Now sub-array contains two elements is already sorted
◦ Repeat the process with the next (unsorted) element. The size of a sorted
sub-array is growing.
◦ Continue until the last element is inserted and the array is sorted
Insertion Sort-Illustration
21 40 5 62 -123 15
21 40 5 62 -123 15
21 40 5 62 -123 15
5 21 40 62 -123 15
5 21 40 62 -123 15
-123 5 21 40 62 15
-123 5 15 21 40 62
Insertion Sort-Pseudocode
Problem : Sort an array of integers in ascending order
Input : Array A of integers, contains n integer
Output : All elements in A are sorted
1. INSERTION-SORT(A)
2. for i = 2 to n do
3. insert the i-th element into the right
position in a sorted sub-array A[1...i-1]
1. INSERTION-SORT(A)
2. for i = 2 to n do
3. j = i - 1
4. key = A[i]
5. while j > 0 AND key < A[j] do
6. A[j+1]=A[j]
7. j = j -1
8. A[j+1] = key
Insertion Sort-Running Time
1. INSERTION-SORT(A)
2. for i = 2 to n do
3. j = i - 1
4. key = A[i]
5. while j > 0 AND key < A[j] do
6. A[j+1]=A[j]
7. j = j -1
8. A[j+1] = key
Running time of insertion sort:
◦ Best case: Line 5 will be excecuted times.
◦ Worst case: Line 5 will be executed times
◦ Average case: Assume for each element, it must traverses half of the already
sorted sub-array, line 5 will be executed times
Running tie of insertion sort:
Sorting in JDK
Sorting primitive types: Java provides Arrays.sort
Example:
1. int[] A= {7,5,61,4,2};
2. Arrays.sort(A);
3. System.our.println(Arrays.toString(A));
//prints 2, 4, 5, 7, 61
Sorting in JDK
To sort an array of object, object must implements comparable and
override method compareTo().
Homeworks
Watch Insertion sort with Romanian Dance
Watch Selection sort Gypsi Folk Dance
Watch Bubble Sort with Hungarian Dance
Sort the array of characters S={E,A,S,Y,Q,U,E,S,T,I,O,N} with insertion,
selection, and bubble sort! (Submit to elearningHW0401.doc or docx)
Answer the insertion sort quiz! (Submit to elearningHW0402.doc or
docx)
Answer the selection sort quiz! (Submit to elearningHW0403.doc or
docx)
Finish the practicuum modul!
Shell Sort-Idea
ShellSort is an upgrade over Insertion Sort (It was created by Donald L.
Shell in 1959):
◦ Selection Sort: moves element efficiently, but does many redundant
comparison
◦ Insertion Sort: minimum number comparison (at average), but inefficient by
moving element one position at a time
◦ ShellSort (Diminishing-increment Sort) is an extension of insertion sort, it allows
exchanging array elements that are far apart
◦ It divides the array into several sub-arrays containing elements far apart (not
contiguous but every h-th element, h is gap between elements) and sort each of
sub-arrays, eventually move small elements relatively in front of others
◦ This process is repeated (with decreasing value of h) and it will produce partially
sorted arrays that can be efficiently sorted, eventually by Insertion Sort
Shell Sort Illustration
Assume we have an array of n integers with random order.
The values of h used is 4, 3 and 1.
The first sub-array {21, -123} with h = 4 is sorted
The second sub-array with h = 4 is sorted
The third sub-array with h = 4 is sorted
21 40 5 62 -123 15
-123 40 5 62 21 15
-123 40 5 62 21 15
-123 15 5 62 21 40
-123 15 5 62 21 40
Shell Sort Illustration(2)
The fourth sub-array with h = 4 is sorted
Continue with h = 3. The first sub-array {-123,62} with h = 3 is sorted
The second sub-array with h = 3 is sorted
The third sub-array with h = 3 is sorted
Finally, sort it with h = 1
-123 15 5 62 21 40
-123 15 5 62 21 40
-123 15 5 62 21 40
-123 15 5 62 21 40
-123 5 15 21 40 62
Shell Sort-Pseudocode
Problem : Sort an array of integers in ascending order
Input : Array A of integers, contains n integer & Array of integers H,
sorted in ascending order
Output : All elements in A are sorted
1. SHELLSORT(A,H)
2. for k = H.length downto 1 do
3. h = H[ k ]
4. divide array into h subarray and sort
each subarray
Shell Sort-Pseudocode(2)
1. SHELLSORT(A,H)
2. for k = H.length downto 1 do
3. h = H[k]
4. for i = h+1 to n do
5. j = i
6. tmp = A[ i ]
7. while(j>=h) AND A[j-h]>temp do
8. A[j] = A[j-h]
9. j = j-h
10. A[j] = tmp
Shell Sort-Running Time
Running time of shell sort depends on the choice of h (gaps):
◦ Best Case:
◦ Worst Case: (Shell,1959), (Sedgewick,1986)

Algorithms and Data Structures - Parahyangan Catholic University Credit Lionov

  • 1.
    Algortihms and Data Structures: Basic Sorting DEPARTMENTOF INFORMATICS PARAHYANGAN CATHOLIC UNIVERSITY CREDIT TO: LIONOV, M.SC
  • 2.
    Definition Sorting is theprocess of rearranging a sequence of objects and put them in some logical order. For this problem to be meaningful, the nature of the list items must allow such an ordering. Computational problem : ◦ Input: A sequence of n numbers ◦ Output: A permutation (reordering) of the input sequence such that
  • 3.
    Definition (2) Example :Given the input sequence {31, 41, 59, 26, 41, 58}, a sorting algorithm returns as output the sequence {26, 31, 41, 41, 58, 59} In practice, not only numbers: characters, strings, collection of data (records) containing a key to be sorted or any other ordered types.
  • 4.
    Motivation Sorting is themost fundamental algorithmic problem in computer science: ◦ Sorting is a starting point to solve other problems and basic building block for many other algorithms (ex: Greedy, Searching) ◦ Design of algorithms similar in sorting are effective in addressing other problems, ex: divide-and-conquer
  • 5.
    Motivation (2) ◦ Historically,computers spent more time to sort than doing anything else. If that fraction is lower today, one likely reason is that sorting algorithms are relatively efficient, not that sorting has diminished in relative importance. ◦ Literally dozens of different algorithms are known, most of which possess some particular advantage over all other algorithms in certain situations. ◦ Sorting plays a major role in commercial data processing and in modern scientific computing.
  • 6.
    Characteristics Stable: ◦ Preserves therelative order of any two equal elements in its input. If an input list contains two equal elements in positions i and j where i < j, then in the sorted list they have to be in positions i’ and j’ respectively, such that i’ < j’. In-place: ◦ An algorithm is said to be in-place if it does not require extra memory, except, possibly, for a few memory units.
  • 7.
    Characteristics (2) Internal-External: ◦ Whetherthere are so many records to be sorted that they must be kept in external files (on disks, tapes, etc) or whether they can all be kept internally in high-speed memory Comparison-based: ◦ Comparison-based sorting works by comparing elements to be sorted. ◦ Non-comparison based sorting: radix sort, bucket sort, count sort, etc.
  • 8.
    Types of SortingAlgorithms Sorting algorithms can be divided based on its design ◦ Exhaustive Search: permutation based sorting ◦ Elementary method : ◦ Brute Force: Selection Sort ◦ Decrease-and-Conquer : Insertion Sort ◦ Uncategorized : Shell Sort, Bubble Sort
  • 9.
    Types of SortingAlgorithms (2) ◦ Divide-and-Conquer : Quick Sort, Merge Sort ◦ Data Structure/Transform-and-Conquer : Heap Sort ◦ Non comparison-based: Count Sort, Radix Sort ◦ Sorting network : Bitonic Sort, Odd-Even Transposition Sort Usually, Bubble Sort is the first sorting algorithm to be taught to the student. However, because it extremely easy to remember and then students tend to use it in every situation, we opt not to teach it in the class, look up by yourself over the internet
  • 10.
    Pragmatics of Sorting Inwhat order do the items should be sorted ? ◦ Increasing or decreasing order ? ◦ Ascending order: for all ◦ Descending order: for all ◦ Sorting just the key or an entire record? Sorting a data set involves maintaining the integrity of complex data records. Example: A mailing list of names, addresses, and phone numbers may be sorted by names as the key field, but it had better retain the linkage between names and addresses.
  • 11.
    Pragmatics of Sorting(2) In what order do the items should be sorted ? ◦ Equal keys? Sometimes the relative order among elements with equal key values is important. Either they have a secondary key or enforce the relative order among these equal keys (stable). ◦ Non-numerical data ? The right way to specify such matters in the sorting algorithm is with an application- specific pairwise-element comparison function.
  • 12.
    Running Time Running timefor Sorting Algorithms usually depend on the number of ◦ comparisons (for comparison-based algorithm) ◦ data movements or exchanges (or array access).
  • 13.
    Running Time (2) Thenumber of comparisons and the number of movements do not have to coincide. Practical reasons must aid in the choice of which algorithm to use: ◦ comparisons become more important if strings or arrays of numbers are compared ◦ exchanges become more important if the data items are large (such as structure, record)
  • 14.
    Running Time Overall, efficiencyis measured for the following cases: ◦ Best case : data already in order ◦ Worst case : data in reverse order ◦ Average case : data in random order
  • 15.
    Selection Sort-Idea Selection sortworks by selecting an element and move it to its proper position : ◦ Assume we have an array of n integers with random order ◦ Find the smallest elements in the array ◦ Exchange the smallest elements with the first element in the array ◦ Starting from the second element, find the smallest element among the last n-1 elements ◦ Exchange it with the second element in the array ◦ Continue until only one element remain unsorted
  • 16.
    Selection Sort-Illustration 21 405 62 -123 15 -123 40 5 62 21 15 -123 5 40 62 21 15 -123 5 15 62 21 40 -123 5 15 21 62 40 -123 5 15 21 40 62
  • 17.
    Selection Sort-Pseudocode Problem :Sort an array of integers in ascending order Input : Array A of integers, contains n integer Output : All elements in A are sorted 1. SELECTION-SORT(A) 2. for i =1 to n-1 do 3. select the i-th smallest element 4. exchange with the i-th element 1. SELECTION-SORT(A) 2. for i =1 to n-1 do 3. min = i 4. for j = i+1 to n do 5. if A[j] < A[min] 6. min = j 7. exchange A[min]with A[i]
  • 18.
    Selection Sort-Running Time 1.SELECTION-SORT(A) 2. for i =1 to n-1 do 3. min = i 4. for j = i+1 to n do 5. if A[j] < A[min] 6. min = j 7. exchange A[min]with A[i] Running time of Selection Sort ◦ Best Case: Line 5 will be excecuted ◦ Average Case: Line 5 will be excecuted ◦ Worst Case: Line 5 will be excecuted Running time of Selection Sort:
  • 19.
    Insertion Sort-Idea Selection sortworks by inserting an element into its proper position in already sorted (sub)-array: ◦ Assume we have an array of n integers with random order ◦ The first element in already sorted ◦ Insert the second element into already sorted sub-array contains the first element ◦ Now sub-array contains two elements is already sorted ◦ Repeat the process with the next (unsorted) element. The size of a sorted sub-array is growing. ◦ Continue until the last element is inserted and the array is sorted
  • 20.
    Insertion Sort-Illustration 21 405 62 -123 15 21 40 5 62 -123 15 21 40 5 62 -123 15 5 21 40 62 -123 15 5 21 40 62 -123 15 -123 5 21 40 62 15 -123 5 15 21 40 62
  • 21.
    Insertion Sort-Pseudocode Problem :Sort an array of integers in ascending order Input : Array A of integers, contains n integer Output : All elements in A are sorted 1. INSERTION-SORT(A) 2. for i = 2 to n do 3. insert the i-th element into the right position in a sorted sub-array A[1...i-1] 1. INSERTION-SORT(A) 2. for i = 2 to n do 3. j = i - 1 4. key = A[i] 5. while j > 0 AND key < A[j] do 6. A[j+1]=A[j] 7. j = j -1 8. A[j+1] = key
  • 22.
    Insertion Sort-Running Time 1.INSERTION-SORT(A) 2. for i = 2 to n do 3. j = i - 1 4. key = A[i] 5. while j > 0 AND key < A[j] do 6. A[j+1]=A[j] 7. j = j -1 8. A[j+1] = key Running time of insertion sort: ◦ Best case: Line 5 will be excecuted times. ◦ Worst case: Line 5 will be executed times ◦ Average case: Assume for each element, it must traverses half of the already sorted sub-array, line 5 will be executed times Running tie of insertion sort:
  • 23.
    Sorting in JDK Sortingprimitive types: Java provides Arrays.sort Example: 1. int[] A= {7,5,61,4,2}; 2. Arrays.sort(A); 3. System.our.println(Arrays.toString(A)); //prints 2, 4, 5, 7, 61
  • 24.
    Sorting in JDK Tosort an array of object, object must implements comparable and override method compareTo().
  • 25.
    Homeworks Watch Insertion sortwith Romanian Dance Watch Selection sort Gypsi Folk Dance Watch Bubble Sort with Hungarian Dance Sort the array of characters S={E,A,S,Y,Q,U,E,S,T,I,O,N} with insertion, selection, and bubble sort! (Submit to elearningHW0401.doc or docx) Answer the insertion sort quiz! (Submit to elearningHW0402.doc or docx) Answer the selection sort quiz! (Submit to elearningHW0403.doc or docx) Finish the practicuum modul!
  • 26.
    Shell Sort-Idea ShellSort isan upgrade over Insertion Sort (It was created by Donald L. Shell in 1959): ◦ Selection Sort: moves element efficiently, but does many redundant comparison ◦ Insertion Sort: minimum number comparison (at average), but inefficient by moving element one position at a time ◦ ShellSort (Diminishing-increment Sort) is an extension of insertion sort, it allows exchanging array elements that are far apart ◦ It divides the array into several sub-arrays containing elements far apart (not contiguous but every h-th element, h is gap between elements) and sort each of sub-arrays, eventually move small elements relatively in front of others ◦ This process is repeated (with decreasing value of h) and it will produce partially sorted arrays that can be efficiently sorted, eventually by Insertion Sort
  • 27.
    Shell Sort Illustration Assumewe have an array of n integers with random order. The values of h used is 4, 3 and 1. The first sub-array {21, -123} with h = 4 is sorted The second sub-array with h = 4 is sorted The third sub-array with h = 4 is sorted 21 40 5 62 -123 15 -123 40 5 62 21 15 -123 40 5 62 21 15 -123 15 5 62 21 40 -123 15 5 62 21 40
  • 28.
    Shell Sort Illustration(2) Thefourth sub-array with h = 4 is sorted Continue with h = 3. The first sub-array {-123,62} with h = 3 is sorted The second sub-array with h = 3 is sorted The third sub-array with h = 3 is sorted Finally, sort it with h = 1 -123 15 5 62 21 40 -123 15 5 62 21 40 -123 15 5 62 21 40 -123 15 5 62 21 40 -123 5 15 21 40 62
  • 29.
    Shell Sort-Pseudocode Problem :Sort an array of integers in ascending order Input : Array A of integers, contains n integer & Array of integers H, sorted in ascending order Output : All elements in A are sorted 1. SHELLSORT(A,H) 2. for k = H.length downto 1 do 3. h = H[ k ] 4. divide array into h subarray and sort each subarray
  • 30.
    Shell Sort-Pseudocode(2) 1. SHELLSORT(A,H) 2.for k = H.length downto 1 do 3. h = H[k] 4. for i = h+1 to n do 5. j = i 6. tmp = A[ i ] 7. while(j>=h) AND A[j-h]>temp do 8. A[j] = A[j-h] 9. j = j-h 10. A[j] = tmp
  • 31.
    Shell Sort-Running Time Runningtime of shell sort depends on the choice of h (gaps): ◦ Best Case: ◦ Worst Case: (Shell,1959), (Sedgewick,1986)