0% found this document useful (0 votes)
97 views

Data Structures and Algorithms (CS F211) Sorting

The document summarizes different sorting algorithms including selection sort, bubble sort, insertion sort, merge sort, and quick sort. It provides pseudocode for each algorithm and discusses their time complexities. It also lists some example problems related to these sorting techniques.

Uploaded by

rohan joshi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
97 views

Data Structures and Algorithms (CS F211) Sorting

The document summarizes different sorting algorithms including selection sort, bubble sort, insertion sort, merge sort, and quick sort. It provides pseudocode for each algorithm and discusses their time complexities. It also lists some example problems related to these sorting techniques.

Uploaded by

rohan joshi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Data Structures and Algorithms (CS F211)

Sorting
Sarthak Moorjani, Samarth Jain
Spring 2019

1 Sorting Techniques
1.1 Selection Sort
The basic idea of selection sorts lies behind the fact that it finds the minimum
element in each iteration (of the unsorted array) and puts the minimum element
into its correct location. The process repeats and finally we get the sorted array.
Technically speaking, every element is compared to every other element and the
time complexity of the algorithm is O(N 2 ). Following is the pseudo-code for
selection sort -
v o i d s e l e c t i o n s o r t ( i n t A[ ] , i n t n ) {
i n t min ;
f o r ( i n t i = 0 ; i < n−1 ; i ++) { .
minimum = i ;
f o r ( i n t j = i +1; j < n ; j++ ) {
i f (A[ j ] < A[ min ] ) {
min = j ;
}
}
swap ( A[ min ] , A[ i ] ) ;
}
If you want, you can try practicing the code here.

1.2 Bubble Sort


Bubble sort is based on the idea of repeatedly comparing pairs of adjacent
elements and then swapping their positions if they exist in the wrong order.
The average and worst case complexity of Bubble sort is O(N 2 ), however with
a simple modification, we can take the best case complexity to be O(N ) by
simply maintaining a variable which tells us if the swap occurred in the current
iteration or not. So if the array is sorted, N − 1 comparisons will happen and
our swap variable will tell us that no swap occurred and hence, we can stop.
This happens when the array is already sorted.

1
Following is the pseudocode for bubble sort -
v o i d b u b b l e s o r t ( i n t A[ ] , i n t n ) {
i n t temp ;
f o r ( i n t k = 0 ; k< n−1; k++) {
f o r ( i n t i = 0 ; i < n−k −1; i ++) {
i f (A[ i ] > A[ i +1] ) {
temp = A[ i ] ;
A[ i ] = A[ i + 1 ] ;
A[ i +1] = temp ;
}
}
}
}
If you want, you can try practicing the code here.

1.3 Insertion Sort


The basic idea of insertion sort lies on the fact that in every iteration, one ele-
ment from the original array is used up and we aim to find its correct location
in the final sorted array. It works similar to how we sort playing cards. The
algorithm iterates the input elements by growing the sorted array at each itera-
tion. The average case complexity of the algorithm is O(N 2 ) and the best case
is O(N ) (Occurs when the array is already sorted). Following is the pseudo-code
for Insertion Sort -
v o i d i n s e r t i o n s o r t ( i n t A[ ] , i n t n )
{
f o r ( i n t i = 0 ; i < n ; i++ ) {
i n t temp = A[ i ] ;
int j = i ;
w h i l e ( j > 0 && temp < A[ j −1]) {
A[ j ] = A[ j − 1 ] ;
j= j − 1 ;
}
A[ j ] = temp ;
}
}
If you want, you can try practicing the code here

2
1.4 Merge Sort
Merge sort is a divide-and-conquer algorithm based on the idea of breaking
down a list into several sub-lists until each sublist consists of a single element
and merging those sublists in a manner that results into a sorted list.
Idea

• Divide the unsorted list into N sublists, each containing 1 element.

• Take adjacent pairs of two singleton lists and merge them to form a list
of 2 elements. N will now convert into N2 lists of size 2.
• Repeat the process till a single sorted list of obtained.

The pseudo-code of the main divide and conquer function of merge sort is
as follows -
v o i d m e r g e s o r t ( i n t A[ ] , i n t s t a r t , i n t end )
{
i f ( s t a r t < end ) {
i n t mid = ( s t a r t + end ) / 2 ;
m e r g e s o r t (A, s t a r t , mid ) ;
m e r g e s o r t (A, mid+1 , end ) ;
merge (A, s t a r t , mid , end ) ;
}
}
Here the merge function simply merges the two arrays. It is important to
note that the time complexity of mergesort algorithm is always O(N logN ). It
is recommended that you try to code this atleast twice. You can try practicing
merge sort here.

1.5 Quick Sort


Quicksort also works in a divide and conquer fashion like merge sort. The idea
of quicksort is to select a pivot element and partition the array in such a way
that all elements to the left of the pivot are less than it and all elements to the
right of it are more than it. Usually we pick up either the first or the last element
as the pivot element. However, you are free to choose the pivot. The average
case time complexity of the quicksort algorithm is O(N logN ). However, in the
worst case, the time complexity can also go to O(N 2 ) which does not happen
in mergesort. Also, Quicksort does not use any auxilliary space unlike merge
sort where an extra O(N ) space is used. So as usual, there is always a tradeoff
between memory and time complexity. A good comparison of mergesort and
quicksort is done here. This is very important from interviews perspective.

3
Here is the pseudo-code for quicksort -
q u i c k s o r t (A, p , r ) {
i f ( p<r )
q = p a r t i t i o n (A, p , r )
q u i c k s o r t (A, p , q−1)
q u i c k s o r t (A, q+1, r )
}
p a r t i t i o n (A, p , r ) {
i = p−1
f o r ( i n t j=p ; j<=r −1; j ++}{
i f (A[ j ]<=A[ r ] ) {
i ++;
swap (A[ i ] ,A[ j ] )
}
}
swap (A[ i +1] ,A[ r ] )
r e t u r n i +1
}

2 Problems
• Bubble Sort related problem Problem Link
• Quick Sort related problem Problem Link
• Descending Sort Problem Link
• The Crofts Game Problem Link

• Chef and Card Trick Problem Link


• Count Inversions. Problem Link
• Interval Overlapping Problem Link

• A Mathematical Problem Problem Link


• Min Swaps Problem Link
• Binary Search question 1 of your lab was a good sorting + binary search
problem.

Thanks to Gunjan, Anish for Problem Links.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy