Sorting Techniques
Amar Jukuntla, Assistant Professor,
VFSTR University
Index
• Introduction
• Selection Sort
• Insertion Sort
• Bubble Sort
• Quick Sort
• Merge Sort
Introduction
•Sorting is a process that organizes a collection of data into
either ascending or descending order.
• An internal sort requires that the collection of data fit
entirely in the computer’s main memory.
• We can use an external sort when the collection of data
cannot fit in the computer’s main memory all at once but must
reside in secondary storage such as on a disk.
• A comparison-based sorting algorithm makes ordering
decisions only on the basis of comparisons.
Internal or External Sort
• In an internal sort, the list of records is small enough to be
maintained entirely in physical memory for the duration of the sort.
• In an external sort, the list of records will not fit entirely
into physical memory at once. In that case, the records are kept in disk
files and only a selection of them are resident in physical memory at
any given time.
Selection Sort
• The list is divided into two sublists, sorted and unsorted, which are
divided by an imaginary wall.
• Find the smallest element from the unsorted sublist and swap
it with the element at the beginning of the unsorted data.
• After each selection and swapping, the imaginary wall
between the two sublists move one element ahead, increasing the number of
sorted elements and decreasing the number of unsorted ones.
• Each time, move one element from the unsorted sublist to the
sorted sublist, we say that we have completed a sort pass.
• A list of n elements requires n-1 passes to completely rearrange the data.
Selection Sort
23 78 45 8 32 56
8 78 45 23 32 56
8 23 45 78 32 56
8 23 32 78 45 56
8 23 32 45 78 56
8 23 32 45 56 78
Original List
After pass 1
After pass 2
After pass 3
After pass 4
After pass 5
Sorted Unsorted
Insertion Sort
• Insertion sort is a simple sorting algorithm that is appropriate for
small inputs.
• Most common sorting technique used by card players.
• The list is divided into two parts: sorted and unsorted.
• In each pass, the first element of the unsorted part is
picked up, transferred to the sorted sublist, and inserted at the
appropriate place.
• A list of n elements will take at most n-1 passes to sort the data.
Insertion Sort
Original List
After pass 1
After pass 2
After pass 3
After pass 4
After pass 5
23 78 45 8 32 56
23 78 45 8 32 56
23 45 78 8 32 56
8 23 45 78 32 56
8 23 32 45 78 56
8 23 32 45 56 78
Sorted Unsorted
Bubble Sort
• The list is divided into two sublists: sortedand unsorted.
• The smallest element is bubbled from the unsorted list and moved
to the sorted sublist.
• After that, the wall moves one element ahead, increasing the number
of sorted elements and decreasing the number of unsorted ones.
• Each time an element moves from the unsorted part to the sorted
part one sort pass is completed.
• Given a list of n elements, bubble sort requires up to n-1 passes to sort the
data.
Quick Sort
• Quicksort is based on the divide-and-conquer paradigm.
• It picks an element as pivot and partitions the given array
around the picked pivot.
• There are many different versions of quickSort that pick pivot in
different ways.
• Always pick first element as pivot.
• Always pick last element as pivot (implemented below)
• Pick a random element as pivot.
• Pick median as pivot.
Quick
PSUEDO
CODE
Example
78 21 14 97 87 62 74 85 76 45 84 22
78 21 14 22 87 62 74 85 76 45 84 97
78 21 14 22 45 62 74 85 76 87 84 97
78 21 14 22 45 62 74 76 85 87 84 97
76 21 14 22 45 62 74 78 85 87 84 97
Merge Sort
• Merge Sort is a Divide and Conquer algorithm.
• It divides input array in two halves, calls itself for the two halves and
then merges the two sorted halves.
Algorithm
Sorting
Sorting

Sorting

  • 1.
    Sorting Techniques Amar Jukuntla,Assistant Professor, VFSTR University
  • 2.
    Index • Introduction • SelectionSort • Insertion Sort • Bubble Sort • Quick Sort • Merge Sort
  • 3.
    Introduction •Sorting is aprocess that organizes a collection of data into either ascending or descending order. • An internal sort requires that the collection of data fit entirely in the computer’s main memory. • We can use an external sort when the collection of data cannot fit in the computer’s main memory all at once but must reside in secondary storage such as on a disk. • A comparison-based sorting algorithm makes ordering decisions only on the basis of comparisons.
  • 4.
    Internal or ExternalSort • In an internal sort, the list of records is small enough to be maintained entirely in physical memory for the duration of the sort. • In an external sort, the list of records will not fit entirely into physical memory at once. In that case, the records are kept in disk files and only a selection of them are resident in physical memory at any given time.
  • 5.
    Selection Sort • Thelist is divided into two sublists, sorted and unsorted, which are divided by an imaginary wall. • Find the smallest element from the unsorted sublist and swap it with the element at the beginning of the unsorted data. • After each selection and swapping, the imaginary wall between the two sublists move one element ahead, increasing the number of sorted elements and decreasing the number of unsorted ones. • Each time, move one element from the unsorted sublist to the sorted sublist, we say that we have completed a sort pass. • A list of n elements requires n-1 passes to completely rearrange the data.
  • 6.
  • 7.
    23 78 458 32 56 8 78 45 23 32 56 8 23 45 78 32 56 8 23 32 78 45 56 8 23 32 45 78 56 8 23 32 45 56 78 Original List After pass 1 After pass 2 After pass 3 After pass 4 After pass 5 Sorted Unsorted
  • 8.
    Insertion Sort • Insertionsort is a simple sorting algorithm that is appropriate for small inputs. • Most common sorting technique used by card players. • The list is divided into two parts: sorted and unsorted. • In each pass, the first element of the unsorted part is picked up, transferred to the sorted sublist, and inserted at the appropriate place. • A list of n elements will take at most n-1 passes to sort the data.
  • 9.
  • 10.
    Original List After pass1 After pass 2 After pass 3 After pass 4 After pass 5 23 78 45 8 32 56 23 78 45 8 32 56 23 45 78 8 32 56 8 23 45 78 32 56 8 23 32 45 78 56 8 23 32 45 56 78 Sorted Unsorted
  • 11.
    Bubble Sort • Thelist is divided into two sublists: sortedand unsorted. • The smallest element is bubbled from the unsorted list and moved to the sorted sublist. • After that, the wall moves one element ahead, increasing the number of sorted elements and decreasing the number of unsorted ones. • Each time an element moves from the unsorted part to the sorted part one sort pass is completed. • Given a list of n elements, bubble sort requires up to n-1 passes to sort the data.
  • 15.
    Quick Sort • Quicksortis based on the divide-and-conquer paradigm. • It picks an element as pivot and partitions the given array around the picked pivot. • There are many different versions of quickSort that pick pivot in different ways. • Always pick first element as pivot. • Always pick last element as pivot (implemented below) • Pick a random element as pivot. • Pick median as pivot.
  • 16.
  • 17.
  • 19.
    Example 78 21 1497 87 62 74 85 76 45 84 22 78 21 14 22 87 62 74 85 76 45 84 97 78 21 14 22 45 62 74 85 76 87 84 97 78 21 14 22 45 62 74 76 85 87 84 97 76 21 14 22 45 62 74 78 85 87 84 97
  • 20.
    Merge Sort • MergeSort is a Divide and Conquer algorithm. • It divides input array in two halves, calls itself for the two halves and then merges the two sorted halves.
  • 21.