SlideShare a Scribd company logo
DATA STRUCTURES
SORTING
Sorting
Sorting
Sorting
• Insertion sort is a simple sorting algorithm that builds the
final sorted array one item at a time.
• Insertion sort keeps a prefix of the array sorted.
• At each step, this prefix is grown by inserting the next
value into it at the correct place.
• Eventually, the prefix is the entire array, which is therefore
sorted.
Algorithm:
1. If it is the first element, it is already sorted. return 1.
2. Pick next element.
3. Compare with all elements in the sorted sub-list.
4. Shift all the elements in the sorted sub-list that is
greater than the
value to be sorted.
5. Insert the value.
6. Repeat until list is sorted.
Advantages:
• The main advantage of the insertion sort is its
simplicity.
• It also exhibits a good performance when dealing with
a small list.
Disadvantages:
• The disadvantage of the insertion sort is that it does
not perform as well as other, better sorting algorithms.
• The insertion sort is particularly useful only when
sorting a list of few items.
Sorting
• The quick sort works on the divide-and-conquer principle.
• Based on partitioning of array of data into smaller arrays.
• First, it partitions the list of items into two sublists based
on a pivot element.
• All elements in the first sublist are arranged to be smaller
than the pivot, while all elements in the second sublist are
arranged to be larger than the pivot.
• The same partitioning and arranging process is performed
repeatedly on the resulting sublists until the whole list of
items are sorted.
Given a pivot, partition the elements of the array such that
the resulting array consists of:
1. One sub-array that contains elements >= pivot
2. Another sub-array that contains elements < pivot
The sub-arrays are stored in the original data array.
Partitioning loops through, swapping elements below/above
pivot.
Sorting
Algorithm:
Given an array of n elements (e.g., integers):
If array only contains one element, return
Else
– pick one element to use as pivot.
– Partition elements into two sub-arrays:
• Elements less than or equal to pivot
• Elements greater than pivot
– Quicksort two sub-arrays
– Return results
40 20 10 30 60 50 7 80 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index
3. If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
Advantage:
• The quick sort is regarded as the best sorting algorithm.
• This is because of its significant advantage in terms of
efficiency because it is able to deal well with a huge list of
items.
Disadvantage:
• Implementation of the algorithm may result in stack
overflow error, since it may require O(n) embedded
recursive calls.
Sorting

More Related Content

PPT
Quick sort data structures
PDF
Linked list implementation of Queue
PPTX
Arrays and linked lists
PPTX
Standard template library
PDF
Page Rank Algorithm
DOCX
List - Operations and Implementation
PPTX
Data structure
PPTX
Insertion and Redix Sort
Quick sort data structures
Linked list implementation of Queue
Arrays and linked lists
Standard template library
Page Rank Algorithm
List - Operations and Implementation
Data structure
Insertion and Redix Sort

What's hot (19)

DOCX
Quick sort
PDF
pseudo code
DOCX
Quick sort
PPTX
Linked stacks and queues
PPTX
PPTX
poornima.coseq
DOC
Assignment Algo
PPTX
Searching,sorting
PPTX
single linked list
PDF
linear search and binary search
PPTX
PPTX
Selection sorting
PDF
searching
PDF
PPTX
8 elementary sorts-insertion
PPTX
Alg II Unit 3-6-solvingsystemsmatrices
PPTX
Alg II 3-6 Solving Systems - Matrices
PPTX
Report cs3 pillos
PPTX
Doubly circular linked list
Quick sort
pseudo code
Quick sort
Linked stacks and queues
poornima.coseq
Assignment Algo
Searching,sorting
single linked list
linear search and binary search
Selection sorting
searching
8 elementary sorts-insertion
Alg II Unit 3-6-solvingsystemsmatrices
Alg II 3-6 Solving Systems - Matrices
Report cs3 pillos
Doubly circular linked list
Ad

Similar to Sorting (20)

PPTX
Sorting Algorithms to arrange data in particular format
PPTX
SORTING techniques.pptx
PPTX
DS PPT - ( 1 )SORTING lgoritham techniques with bast example
PDF
BCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHI
PPTX
DS Module1 (1).pptx
PDF
linked_list.pdf [for undergraduate students
PPTX
data_structure_Chapter two_computer.pptx
PPTX
Data structure and algorithms
PDF
Data Structure & Algorithms - Operations
PPTX
Different type of Arrays and multidimensional arrays
PDF
Heap, quick and merge sort
PPTX
what is sorting algorithm and implementation.pptx
PPT
lecture 02.2.ppt
PPTX
Sorting types and Algorithms
PPTX
sortting 2 sderftgyurvtynurftgyhujse.pptx
PPTX
Sorting.pptx
PPTX
DS - Unit 2 FINAL (2).pptx
PPTX
Data Structures_ Sorting & Searching
PPTX
Insertion sort & Linear Search presentation
PPTX
1.Introduction to Data Structures and Algorithms.pptx
Sorting Algorithms to arrange data in particular format
SORTING techniques.pptx
DS PPT - ( 1 )SORTING lgoritham techniques with bast example
BCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHI
DS Module1 (1).pptx
linked_list.pdf [for undergraduate students
data_structure_Chapter two_computer.pptx
Data structure and algorithms
Data Structure & Algorithms - Operations
Different type of Arrays and multidimensional arrays
Heap, quick and merge sort
what is sorting algorithm and implementation.pptx
lecture 02.2.ppt
Sorting types and Algorithms
sortting 2 sderftgyurvtynurftgyhujse.pptx
Sorting.pptx
DS - Unit 2 FINAL (2).pptx
Data Structures_ Sorting & Searching
Insertion sort & Linear Search presentation
1.Introduction to Data Structures and Algorithms.pptx
Ad

Recently uploaded (20)

PDF
Business Analytics and business intelligence.pdf
PDF
annual-report-2024-2025 original latest.
PPT
Reliability_Chapter_ presentation 1221.5784
PDF
168300704-gasification-ppt.pdfhghhhsjsjhsuxush
PPTX
Qualitative Qantitative and Mixed Methods.pptx
PDF
Mega Projects Data Mega Projects Data
PPTX
Supervised vs unsupervised machine learning algorithms
PPTX
advance b rammar.pptxfdgdfgdfsgdfgsdgfdfgdfgsdfgdfgdfg
PPTX
climate analysis of Dhaka ,Banglades.pptx
PPTX
STUDY DESIGN details- Lt Col Maksud (21).pptx
PPT
Quality review (1)_presentation of this 21
PPTX
Introduction to machine learning and Linear Models
PPTX
MODULE 8 - DISASTER risk PREPAREDNESS.pptx
PPTX
IBA_Chapter_11_Slides_Final_Accessible.pptx
PPTX
Introduction to Knowledge Engineering Part 1
PPTX
Computer network topology notes for revision
PPTX
ALIMENTARY AND BILIARY CONDITIONS 3-1.pptx
PDF
Lecture1 pattern recognition............
PPTX
iec ppt-1 pptx icmr ppt on rehabilitation.pptx
PPTX
Business Ppt On Nestle.pptx huunnnhhgfvu
Business Analytics and business intelligence.pdf
annual-report-2024-2025 original latest.
Reliability_Chapter_ presentation 1221.5784
168300704-gasification-ppt.pdfhghhhsjsjhsuxush
Qualitative Qantitative and Mixed Methods.pptx
Mega Projects Data Mega Projects Data
Supervised vs unsupervised machine learning algorithms
advance b rammar.pptxfdgdfgdfsgdfgsdgfdfgdfgsdfgdfgdfg
climate analysis of Dhaka ,Banglades.pptx
STUDY DESIGN details- Lt Col Maksud (21).pptx
Quality review (1)_presentation of this 21
Introduction to machine learning and Linear Models
MODULE 8 - DISASTER risk PREPAREDNESS.pptx
IBA_Chapter_11_Slides_Final_Accessible.pptx
Introduction to Knowledge Engineering Part 1
Computer network topology notes for revision
ALIMENTARY AND BILIARY CONDITIONS 3-1.pptx
Lecture1 pattern recognition............
iec ppt-1 pptx icmr ppt on rehabilitation.pptx
Business Ppt On Nestle.pptx huunnnhhgfvu

Sorting

  • 5. • Insertion sort is a simple sorting algorithm that builds the final sorted array one item at a time. • Insertion sort keeps a prefix of the array sorted. • At each step, this prefix is grown by inserting the next value into it at the correct place. • Eventually, the prefix is the entire array, which is therefore sorted.
  • 6. Algorithm: 1. If it is the first element, it is already sorted. return 1. 2. Pick next element. 3. Compare with all elements in the sorted sub-list. 4. Shift all the elements in the sorted sub-list that is greater than the value to be sorted. 5. Insert the value. 6. Repeat until list is sorted.
  • 7. Advantages: • The main advantage of the insertion sort is its simplicity. • It also exhibits a good performance when dealing with a small list. Disadvantages: • The disadvantage of the insertion sort is that it does not perform as well as other, better sorting algorithms. • The insertion sort is particularly useful only when sorting a list of few items.
  • 9. • The quick sort works on the divide-and-conquer principle. • Based on partitioning of array of data into smaller arrays. • First, it partitions the list of items into two sublists based on a pivot element. • All elements in the first sublist are arranged to be smaller than the pivot, while all elements in the second sublist are arranged to be larger than the pivot. • The same partitioning and arranging process is performed repeatedly on the resulting sublists until the whole list of items are sorted.
  • 10. Given a pivot, partition the elements of the array such that the resulting array consists of: 1. One sub-array that contains elements >= pivot 2. Another sub-array that contains elements < pivot The sub-arrays are stored in the original data array. Partitioning loops through, swapping elements below/above pivot.
  • 12. Algorithm: Given an array of n elements (e.g., integers): If array only contains one element, return Else – pick one element to use as pivot. – Partition elements into two sub-arrays: • Elements less than or equal to pivot • Elements greater than pivot – Quicksort two sub-arrays – Return results
  • 13. 40 20 10 30 60 50 7 80 100 [0] [1] [2] [3] [4] [5] [6] [7] [8] too_big_index too_small_index 1. While data[too_big_index] <= data[pivot] ++too_big_index 2. While data[too_small_index] > data[pivot] --too_small_index 3. If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index]
  • 14. Advantage: • The quick sort is regarded as the best sorting algorithm. • This is because of its significant advantage in terms of efficiency because it is able to deal well with a huge list of items. Disadvantage: • Implementation of the algorithm may result in stack overflow error, since it may require O(n) embedded recursive calls.