0% found this document useful (0 votes)
32 views14 pages

Overview of Sorting Algorithms

The document provides an overview of sorting algorithms, explaining their importance in computer science and their objectives, such as improving search efficiency and data visualization. It details various types of sorting algorithms, including Selection Sort, Insertion Sort, Quick Sort, and Merge Sort, along with their algorithms, properties, and example implementations in C. Each sorting method is described with its process, advantages, and a step-by-step explanation of how it operates.

Uploaded by

Priyanshu Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views14 pages

Overview of Sorting Algorithms

The document provides an overview of sorting algorithms, explaining their importance in computer science and their objectives, such as improving search efficiency and data visualization. It details various types of sorting algorithms, including Selection Sort, Insertion Sort, Quick Sort, and Merge Sort, along with their algorithms, properties, and example implementations in C. Each sorting method is described with its process, advantages, and a step-by-step explanation of how it operates.

Uploaded by

Priyanshu Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Introduction to Sorting

Sorting is the process of arranging data elements in a particular order, typically in ascending or descending
order. It is one of the most fundamental operations in computer science and is widely used in searching, data
analysis, database operations, and optimization problems.

Objectives of Sorting

• To arrange data in a meaningful order

• To improve the efficiency of searching algorithms

• To simplify data visualization and processing

Properties of Sorting Algorithms

1. Stability: A sorting algorithm is stable if it preserves the relative order of equal elements.

2. In-place: Uses only a small, constant amount of extra memory.

3. Time Complexity: Time taken by the algorithm (best, average, worst case).

4. Space Complexity: Memory required apart from the input data.

Types of Sorting Algorithms

Sorting algorithms are broadly classified into two categories:

1. Internal Sorting

Sorting performed entirely within the main memory. Examples: Insertion Sort, Selection Sort, Bubble Sort,
Quick Sort, Merge Sort.

2. External Sorting

Sorting performed when data is too large to fit in memory; uses disks. Example: External Merge Sort.
Selection Sort
1. Introduction
Selection Sort is one of the simplest comparison-based sorting algorithms.
It works by repeatedly selecting the smallest element from the unsorted portion of the array and placing it at
the beginning.
It is easy to understand and implement, making it excellent for beginners.

2. Key Idea
Selection sort divides the array into two parts:
1. Sorted Part (left side)
2. Unsorted Part (right side)
Process:
• Find the minimum element in the unsorted part.
• Swap it with the first element of the unsorted part.
• Increase the sorted part by one element.
• Repeat until all elements become sorted.

Example
Let the array be:

29 10 14 37 14

Pass 1
Unsorted:

29 10 14 37 14

Minimum element = 10
Swap 10 with 29

10 29 14 37 14

Pass 2
Unsorted:

29 14 37 14

Minimum element = 14
Swap 14 with 29 → [10, 14, 29, 37, 14]

10 14 29 37 14
Pass 3
Unsorted:

29 37 14

Minimum element = 14
Swap 14 with 29

10 14 14 37 29

Pass 4
Unsorted:

37 29

Minimum element = 29
Swap 29 with 37

10 14 14 29 37

Final Sorted Array

10 14 14 29 37

Algorithm of Selection Sort


SelectionSort(A, n)
1. Repeat for i = 0 to n−2
2. minIndex = i
3. For j = i+1 to n−1
4. If A[j] < A[minIndex]
5. minIndex = j
6. Swap A[i] and A[minIndex]
7. End

Explanation of Algorithm
• Outer Loop (i):
Tracks the first index of the unsorted part.
• Inner Loop (j):
Searches for the minimum in the unsorted part.
• minIndex:
Stores the index of the smallest element found so far.
• Swap:
Places the found minimum at position i.
Selection Sort Program in C
#include <stdio.h>
void selectionSort(int arr[], int n) {

int i, j, minIndex, temp;


for (i = 0; i < n - 1; i++) {
minIndex = i; // Assume current index has the minimum value

// Inner loop to find minimum element in unsorted part


for (j = i + 1; j < n; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j; // Update index of minimum element
}
}
// Swap the found minimum element with the element at i
temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
}

void printArray(int arr[], int n) {


for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}

int main() {
int arr[] = {29, 10, 14, 37, 14};
int n = sizeof(arr) / sizeof(arr[0]);

printf("Original array: ");


printArray(arr, n);

selectionSort(arr, n);

printf("Sorted array: ");


printArray(arr, n);
return 0;
}
Insertion sort

It is a simple sorting algorithm that works by iteratively inserting each element of an unsorted list into its
correct position in a sorted portion of the list.

It is like sorting playing cards in your hands. You split the cards into two groups: the sorted cards and the
unsorted cards. Then, you pick a card from the unsorted group and put it in the right place in the sorted
group.

Insertion sort maintains two parts:

1. Sorted Part (left side)


2. Unsorted Part (right side)
Process:
• Pick the first element of the unsorted part.
• Insert it in the correct position in the sorted part.
• Shift elements (instead of swapping) to make space.
• Repeat until all elements are sorted.

Example
Algorithm of Insertion Sort

InsertionSort(A, n)

1. Repeat for i = 1 to n−1


2. key = A[i]
3. j = i − 1
4. While j ≥ 0 and A[j] > key
5. A[j+1] = A[j] // Shift element right
6. j=j−1
7. A[j+1] = key // Insert key

Explanation of Algorithm
• i represents the first element of the unsorted part.
• key is the element we want to insert in the correct position.
• j scans backward through the sorted part.
• Elements greater than key are shifted to the right.
• Finally, key is inserted in the correct position.

Insertion Sort in C Program

#include <stdio.h>

void insertionSort(int arr[], int n)


{
int i, key, j;

for (i = 1; i < n; i++)


{
key = arr[i]; // Element to be inserted
j = i - 1;

// Shift elements of sorted part that are greater than key


while (j >= 0 && arr[j] > key)
{
arr[j + 1] = arr[j];
j--;
}

// Insert key into its correct position


arr[j + 1] = key;
}
}

void printArray(int arr[], int n)


{
for (int i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
}
int main()
{
int arr[] = {12, 11, 13, 5, 6};
int n = sizeof(arr) / sizeof(arr[0]);

printf("Original array: ");


printArray(arr, n);

insertionSort(arr, n);

printf("Sorted array: ");


printArray(arr, n);

return 0;
}
Quick Sort Algorithm

Quick Sort is a divide-and-conquer based sorting algorithm known for its excellent average-case
performance.
Main Idea:
1. Select a pivot element.
2. Partition the array into two halves:
o Elements less than pivot go to the left.
o Elements greater than pivot go to the right.
3. Recursively apply Quick Sort to each half.

Important Properties

Feature Description
Type Internal sorting
Technique Divide and conquer
Stability Unstable (positions of equal elements may change)
In-place Yes (does not need extra array)

Steps of Quick Sort


1. Choose a pivot (commonly the last element).
2. Call Partition() to rearrange the array.
3. Recursively sort left and right subarrays.

Quick Sort Algorithm (Pseudocode)


QUICKSORT(arr, low, high):
if (low < high):
p = PARTITION(arr, low, high)
QUICKSORT(arr, low, p - 1)
QUICKSORT(arr, p + 1, high)

Partition Algorithm
PARTITION(arr, low, high):
pivot = arr[high]
i = low - 1
for j = low to high - 1:
if arr[j] <= pivot:
i=i+1
swap arr[i] and arr[j]
swap arr[i+1] and arr[high]
return (i + 1)

Time and Space Complexity


Case Time
Best Case O(n log n)
Average Case O(n log n)
Worst Case (already sorted array, poor pivot) O(n²)
Dry Run of Partition() on Following Number
Initial Array
Value 20 15 6 12 8 25 30 2 14
Index 0 1 2 3 4 5 6 7 8

Pivot = 14
i= -1
Step-by-step Iteration Table
j arr[j] Compare arr[j] ≤ 14 Action
0 20 No Do nothing
1 15 No Do nothing
j = 2 → arr[2]=6 ≤ pivot
i=-1 → i++ → i=0
2 6 Yes Swap arr[i] ↔ arr[j]
[6, 15, 20, 12, 8, 25, 30, 2, 14]

j = 3 → arr[3]=12 ≤ pivot
i=0 → i++ → i=1
3 12 Yes Swap arr[1] ↔ arr[3]
[6, 12, 20, 15, 8, 25, 30, 2, 14]

j = 4 → arr[4]=8 ≤ pivot
i=1 → i++ → i=2
4 8 Yes Swap arr[2] ↔ arr[4]
[6, 12, 8, 15, 20, 25, 30, 2, 14]

5 25 No Do nothing
6 30 No Do nothing
j = 7 → arr[7]=2 ≤ pivot
i=2 → i++ → i=3
7 2 Yes Swap arr[3] ↔ arr[7]
[6, 12, 8, 2, 20, 25, 30, 15, 14]

Final Step: Place Pivot at Correct Position


Pivot = 14
Swap arr[i+1] ↔ arr[8] → swap arr[4] and arr[8]
[6, 12, 8, 2, 14, 25, 30, 15, 20]

Output of Partition()
• Partition index = i + 1 = 4
• Pivot 14 is now at its correct sorted position.
Final Partitioned Array
6, 12, 8, 2, 14, 25, 30, 15, 20
Value 6 12 8 2 14 25 30 15 20
Index 0 1 2 3 4 5 6 7 8

Left side (<14):


6, 12, 8, 2
Right side (>14):
25, 30, 15, 20

Similarly Partition function will be called on Left side and Right side arrays of Pivot index 4, until the
whole array is sorted.
Merge Sort Algorithm
Merge Sort is a Divide and Conquer based sorting algorithm.

It works by:
1. Dividing the array into two halves,
2. Sorting each half recursively,
3. Merging the two sorted halves.

Characteristics
• Guaranteed O(n log n) time complexity
• Very efficient for large datasets
• Stable sort (preserves order of equal elements)
• Works well for linked lists also.

Merge Sort – Main Idea

Divide
Split the array into two halves repeatedly until each sub-array has one element.

Conquer
Sort the small arrays recursively using merge sort.

Combine
Merge the sorted arrays into a larger sorted array.

Merge Sort Algorithm (Step-by-Step)


Function: mergeSort(arr, left, right)
if left < right
Find mid = (left + right) / 2
Call mergeSort( arr, left, mid)
Call mergeSort( arr, mid+1, right)
Call merge( arr, left, mid, right)

Function: merge(arr, left, mid, right)


1. Create two temporary arrays:
• Left part: L[]
• Right part: R[]
2. Compare elements from L and R, and put smaller element back into original array.
3. If one list is finished, copy remaining elements from the other list.
Merge Sort C Code
#include <stdio.h>

void merge(int arr[], int left, int mid, int right) {


int n1 = mid - left + 1;
int n2 = right - mid;

int L[n1], R[n2];

// Copy data
for (int i = 0; i < n1; i++)
L[i] = arr[left + i];

for (int j = 0; j < n2; j++)


R[j] = arr[mid + 1 + j];

int i = 0, j = 0, k = left;

// Merging arrays
while (i < n1 && j < n2) {
if (L[i] <= R[j])
arr[k++] = L[i++];
else
arr[k++] = R[j++];
}

// Copy remaining data


while (i < n1) arr[k++] = L[i++];
while (j < n2) arr[k++] = R[j++];
}

void mergeSort(int arr[], int left, int right) {


if (left < right) {
int mid = (left + right) / 2;

mergeSort(arr, left, mid);


mergeSort(arr, mid + 1, right);

merge(arr, left, mid, right);


}
}
Merge Sort Example
Let the array be:
[38, 27, 43, 3, 9, 82, 10]

Step 1: Divide
[38 27 43 3] [9 82 10]

Break left half:


[38 27] [43 3]
→ Break again
[38] [27] [43] [3]

Break right half:


[9] [82 10]
→ Break
[9] [82] [10]

Now every sub-array has one element.

Step 2: Merge Step-by-Step

Merge [38] and [27]


Compare 38 and 27 → smaller is 27
[27, 38]

Merge [43] and [3]


Compare 43 and 3 → smaller is 3
[3, 43]

Merge [27 38] and [3 43]


Step-by-step:
Compare Smaller Result
27 vs 3 3 [3]
27 vs 43 27 [3, 27]
38 vs 43 38 [3, 27, 38]
Remaining → 43
Final:
[3, 27, 38, 43]

Merge [82] and [10]


[10, 82]

Merge [9] and [10 82]


Step-by-step:
• 9 < 10 → [9]
• Remaining → [10, 82]
Final:
[9, 10, 82]
Final Merge of two big halves
Merge:
Left : [3, 27, 38, 43]
Right: [9, 10, 82]
Comparison table:
Compare Smaller Result
3 vs 9 3 [3]
27 vs 9 9 [3, 9]
27 vs 10 10 [3, 9, 10]
27 vs 82 27 [3, 9, 10, 27]
38 vs 82 38 [3, 9, 10, 27, 38]
43 vs 82 43 [3, 9, 10, 27, 38, 43]
Remaining → 82

Final sorted array:


[3, 9, 10, 27, 38, 43, 82]

7. Time Complexity
Case Time
Best O(n log n)
Average O(n log n)
Worst O(n log n)
Space Complexity: O(n) (due to temporary arrays)

You might also like