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

Lecture 4.Pptx

The document provides an overview of four sorting algorithms: Selection Sort, Bubble Sort, Insertion Sort, and Quick Sort, detailing how each algorithm works and providing corresponding C++ code examples. Each sorting method is explained step-by-step, highlighting the process of sorting an array. Additionally, a final comparison table is included, though its contents are not specified in the provided text.

Uploaded by

aleezaarshad397
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)
5 views

Lecture 4.Pptx

The document provides an overview of four sorting algorithms: Selection Sort, Bubble Sort, Insertion Sort, and Quick Sort, detailing how each algorithm works and providing corresponding C++ code examples. Each sorting method is explained step-by-step, highlighting the process of sorting an array. Additionally, a final comparison table is included, though its contents are not specified in the provided text.

Uploaded by

aleezaarshad397
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/ 11

Sorting Algorithms

Selection Sort
How It Works?
• Find the smallest element in the array and swap it
with the first element.
• Move to the second position, find the next smallest
element, and swap it with the second element.
• Repeat this process until the entire array is sorted.

2
Code
#include <iostream>
using namespace std;

int main() {

int arr[] = {29, 10, 14, 37, 13};


int n = sizeof(arr) / sizeof(arr[0]);
for (int i = 0; i < n - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
swap(arr[i], arr[minIndex]);
}
cout << "Sorted Array (Selection Sort): ";
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
return 0;

}
3
Bubble Sort
How It Works?
• Compare adjacent elements and swap if they are in
the wrong order.
• After each full pass, the largest element moves to its
correct position.
• Repeat the process until no swaps are needed.

4
Code
#include <iostream>
using namespace std;

int main() {
int arr[] = {5, 3, 8, 4, 2};
int n = sizeof(arr) / sizeof(arr[0]);

for (int i = 0; i < n - 1; i++) {


bool swapped = false;
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
swap(arr[j], arr[j + 1]);
swapped = true;
}
}
if (!swapped) break; // Optimization: Stop if already sorted
}

cout << "Sorted Array (Bubble Sort): ";


for (int i = 0; i < n; i++)
cout << arr[i] << " ";

return 0;
} 5
Insertion Sort
How It Works?
• Start from the second element and compare it with
previous elements.
• Shift larger elements to the right and insert the
selected element in the correct place.
• Repeat until the entire array is sorted.

6
Code
#include <iostream>
using namespace std;

int main() {
int arr[] = {9, 5, 1, 4, 3};
int n = sizeof(arr) / sizeof(arr[0]);

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


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

cout << "Sorted Array (Insertion Sort): ";


for (int i = 0; i < n; i++)
cout << arr[i] << " ";

return 0;
}
7
Quick Sort
How It Works?
• Pick a pivot element (usually last or random).
• Arrange elements smaller than the pivot to its left and
greater than the pivot to its right (Partitioning step).
• Recursively apply the same process to left and right
subarrays.

8
Code
#include <iostream>
using namespace std;

int partition(int arr[], int start, int end) {


int pivot = arr[end];
int i = start - 1;

for (int j = start; j < end; j++) {


if (arr[j] < pivot) {
i++;
swap(arr[i], arr[j]);
}
}
swap(arr[i + 1], arr[end]);
return i + 1;
}

void quickSort(int arr[], int start, int end) {


if (start < end) {
int pivotIndex = partition(arr, start, end);
quickSort(arr, start, pivotIndex - 1);
quickSort(arr, pivotIndex + 1, end);
}
} 9
Conti....
int main() {
int arr[] = {10, 7, 8, 9, 1, 5};
int n = sizeof(arr) / sizeof(arr[0]);

quickSort(arr, 0, n - 1);

cout << "Sorted Array (Quick Sort): ";


for (int i = 0; i < n; i++)
cout << arr[i] << " ";

return 0;
}

10
Final Comparison Table

11

You might also like