0% found this document useful (0 votes)
7 views9 pages

Wa0008.

The document provides an overview of sorting algorithms in C programming, including Bubble Sort, Insertion Sort, Selection Sort, Quick Sort, and Merge Sort. It explains the definitions, algorithms, and code implementations for each sorting method, highlighting their use cases and efficiency. The conclusion emphasizes that the choice of sorting algorithm depends on specific constraints such as space and time complexity.

Uploaded by

s36347032
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)
7 views9 pages

Wa0008.

The document provides an overview of sorting algorithms in C programming, including Bubble Sort, Insertion Sort, Selection Sort, Quick Sort, and Merge Sort. It explains the definitions, algorithms, and code implementations for each sorting method, highlighting their use cases and efficiency. The conclusion emphasizes that the choice of sorting algorithm depends on specific constraints such as space and time complexity.

Uploaded by

s36347032
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
You are on page 1/ 9

Sorting in C Programming

for Problem Solving

ES-CS201

Name – Sayan Karmakar


University Roll – 13000324120
Department of ECE
CONTENTS

 Introduction
 Bubble Sort
 Insertion Sort
 Selection Sort
 Quick Sort
 Merge Sort
INTRODUCTION
 Definition:
Sorting is the process of arranging elements in a specific order
(ascending/descending).

 Why Sorting?
• Efficient searching (e.g., Binary Search)
• Data organization
• Faster processing

 Types of Sorting Algorithms:


• Comparison-based: Bubble Sort, Selection Sort, Insertion Sort, Merge
Sort, Quick Sort, Heap Sort
• Non-comparison-based: Counting Sort, Radix Sort, Bucket Sort
BUBBLE SORT
 Definition
•A simple sorting algorithm that repeatedly steps through the list, compares
adjacent elements, and swaps them if they are in the wrong order.

 Algorithm  Code
1.Iterate through the array. void bubbleSort(int arr[], int n) {
2.Compare adjacent elements. for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++)
3.Swap if necessary. {
if (arr[j] > arr[j + 1]) {
4.Repeat until the array is sorted. int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
INSERTION SORT
 Definition
•Builds the sorted array one item at a time.
•Picks an element and places it in the correct position.

 Algorithm  Code
1.Start with the second element. void insertionSort(int arr[], int n) {
for (int i = 1; i < n; i++) {
2.Compare it with previous elements. int key = arr[i];
3.Insert it in the correct position. int j = i - 1;
while (j >= 0 && arr[j] > key) {
4.Repeat for all elements. arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}
}
SELECTION SORT
 Definition
•Finds the smallest element and moves it to the beginning.
•Repeats for the remaining unsorted part.

 Algorithm  Code
1.Find the minimum element. void selectionSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
2.Swap it with the first unsorted element. int minIndex = i;
3.Move to the next unsorted element. for (int j = i + 1; j < n; j++)
{
4.Repeat until sorted. if (arr[j] < arr[minIndex])
{
minIndex = j;
}
}
int temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
QUICK SORT
 Definition  Code
•Uses divide and conquer. int partition(int arr[], int low, int high) {
int pivot = arr[high];
•Picks a pivot, partitions the array int i = low - 1;
for (int j = low; j < high; j++) {
•Sorts recursively. if (arr[j] < pivot) {
i++;
int temp = arr[i];
arr[i] = arr[j];
 Algorithm }
arr[j] = temp;

1.Choose a pivot. }
int temp = arr[i + 1];
2.Partition the array into two subarrays. arr[i + 1] = arr[high];
arr[high] = temp;
3.Recursively apply quicksort. }
return i + 1;

4.Combine sorted subarrays. void quickSort(int arr[], int low, int high) {
if (low < high) {
int pi = partition(arr, low, high);
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
MERGE SHORT
 Definition  Code
void merge(int arr[], int l, int m, int r) {
•Uses divide and conquer. int n1 = m - l + 1;
int n2 = r - m;
•Splits the array, sorts each half, and int L[n1], R[n2];
for (int i = 0; i < n1; i++)
merges them. L[i] = arr[l + i];
for (int i = 0; i < n2; i++)
R[i] = arr[m + 1 + i];
int i = 0, j = 0, k = l;
while (i < n1 && j < n2) {
 Algorithm if (L[i] <= R[j]) {
arr[k] = L[i];
1.Divide the array into two halves. i++;
} else {
2.Recursively sort each half. arr[k] = R[j];
j++;
}
3.Merge sorted halves. k++;
}
while (i < n1) arr[k++] = L[i++];
while (j < n2) arr[k++] = R[j++];
}
void mergeSort(int arr[], int l, int r) {
if (l < r) {
int m = l + (r - l) / 2;
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
merge(arr, l, m, r);
}
}
CONCLUSION
 Different sorting algorithms suit different use cases.

 Quick Sort and Merge Sort are efficient for large data sets.

 Bubble Sort, Insertion Sort, and Selection Sort are useful for small or
nearly sorted data.

 Choosing the right algorithm depends on constraints like space and


time complexity.

Thank You

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