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

Sorting

The document provides implementations of five sorting algorithms: Merge Sort, Quick Sort, Bubble Sort, Insertion Sort, and Selection Sort in C++. Each algorithm is explained with its respective code, demonstrating how to sort an array of integers. The main functions for each sorting method showcase examples of sorting arrays and printing the sorted results.

Uploaded by

farhafouadd
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)
2 views

Sorting

The document provides implementations of five sorting algorithms: Merge Sort, Quick Sort, Bubble Sort, Insertion Sort, and Selection Sort in C++. Each algorithm is explained with its respective code, demonstrating how to sort an array of integers. The main functions for each sorting method showcase examples of sorting arrays and printing the sorted results.

Uploaded by

farhafouadd
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/ 7

Merge sort

#include <iostream>
using namespace std;

// Function to merge two halves


void merge(int A[], int low, int mid, int high) {
int i = low, j = mid + 1, k = 0;
int temp[high - low + 1];

while (i <= mid && j <= high) {


if (A[i] < A[j]) {
temp[k++] = A[i++];
} else {
temp[k++] = A[j++];
}
}

while (i <= mid) {


temp[k++] = A[i++];
}

while (j <= high) {


temp[k++] = A[j++];
}

for (i = low, k = 0; i <= high; i++, k++) {


A[i] = temp[k];
}
}

// Recursive Merge Sort function


void mergeSort(int A[], int low, int high) {
if (low < high) {
int mid = (low + high) / 2;
mergeSort(A, low, mid);
mergeSort(A, mid + 1, high);
merge(A, low, mid, high);
}
}

int main() {
int arraySize = 7;
int A[7] = {38, 27, 43, 3, 9, 82, 10};

int low = 0;
int high = arraySize - 1;

mergeSort(A, low, high);

for (int i = 0; i < arraySize; i++) {


cout << A[i] << " ";
}

return 0;
}

Quick sort
#include <iostream>
using namespace std;

// Partition function divides the array and returns the pivot index
int Partition(int A[], int low, int high) {
int pivot = A[low]; // Choose the first element as the pivot
int leftwall = low; // The "wall" separates elements smaller than
pivot

// Loop through the rest of the array


for (int i = low + 1; i <= high; i++) {
if (A[i] < pivot) {
leftwall++; // Move the wall one step to the right
swap(A[i], A[leftwall]); // Put the smaller element on the left side
}
}

// Put the pivot in its correct position (middle of the smaller and larger
elements)
swap(A[low], A[leftwall]);

// Return the final position of the pivot


return leftwall;
}

// The Quicksort function recursively sorts the array


void Quicksort(int A[], int low, int high) {
// Base condition: only proceed if there are at least 2 elements
if (low < high) {
// Partition the array and get the index of the pivot
int pivot_location = Partition(A, low, high);

// Recursively sort the left part (before pivot)


Quicksort(A, low, pivot_location - 1);

// Recursively sort the right part (after pivot)


Quicksort(A, pivot_location + 1, high);
}
}

// Main function to test the quicksort


int main() {
// Example array to sort
int A[] = {33, 10, 55, 71, 29, 3, 17};
int size = sizeof(A) / sizeof(A[0]); // Get number of elements in the
array
// Print array before sorting
cout << "Before Sorting: ";
for (int i = 0; i < size; i++)
cout << A[i] << " ";
cout << endl;

// Call quicksort on the whole array


Quicksort(A, 0, size - 1);

// Print array after sorting


cout << "After Sorting: ";
for (int i = 0; i < size; i++)
cout << A[i] << " ";
cout << endl;

return 0;
}

Bubble Sort
#include <iostream>
using namespace std;

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


int k, r, temp;
for (k = 0; k < n - 1; k++) {
for (r = k + 1; r < n; r++) {
if (arr[k] > arr[r]) {
temp = arr[k];
arr[k] = arr[r];
arr[r] = temp;
}
}
}
}

int main() {
int a[10];
int k;

cout << "Enter 10 elements:\n";


for (k = 0; k < 10; k++) {
cin >> a[k];
}

int n = sizeof(a) / sizeof(a[0]);

bubbleSort(a, n);

cout << "Sorted array:\n";


for (k = 0; k < 10; k++) {
cout << a[k] << " ";
}

return 0;
}

Insertion Sort

#include <iostream>
using namespace std;

void insertionSort(int A[], int n) {


for (int i = 1; i < n; i++) {
int key = A[i]; // The current element to be inserted
int j = i - 1;
// Shift elements of A[0..i-1] that are greater than key to the right
while (j >= 0 && A[j] > key) {
A[j + 1] = A[j]; // Move element one step to the right
j = j - 1;
}

A[j + 1] = key; // Insert the key in the right position


}
}

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

insertionSort(A, n);

cout << "Sorted array: ";


for (int i = 0; i < n; i++) {
cout << A[i] << " ";
}

return 0;
}

SelectionSort

#include <iostream>
using namespace std;

void selectionSort(int A[], int n) {


for (int i = 0; i < n - 1; i++) {
int minIndex = i; // Assume the current index has the minimum
// Search for the smallest element in the rest of the array
for (int j = i + 1; j < n; j++) {
if (A[j] < A[minIndex]) {
minIndex = j; // Update index of the minimum
}
}

// Swap if a smaller element was found


if (minIndex != i) {
int temp = A[i];
A[i] = A[minIndex];
A[minIndex] = temp;
}
}
}

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

selectionSort(A, n);

cout << "Sorted array: ";


for (int i = 0; i < n; i++) {
cout << A[i] << " ";
}

return 0;
}

You might also like