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

Sorting

The document provides implementations of five sorting algorithms: Merge Sort, Quick Sort, Bubble Sort, Selection Sort, and Insertion Sort, each with accompanying C++ code. It includes the main function for each algorithm to demonstrate sorting an array and printing the results before and after sorting. The document serves as a practical guide for understanding and applying these sorting techniques in programming.

Uploaded by

abhi22jeetu
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 views7 pages

Sorting

The document provides implementations of five sorting algorithms: Merge Sort, Quick Sort, Bubble Sort, Selection Sort, and Insertion Sort, each with accompanying C++ code. It includes the main function for each algorithm to demonstrate sorting an array and printing the results before and after sorting. The document serves as a practical guide for understanding and applying these sorting techniques in programming.

Uploaded by

abhi22jeetu
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

Sorting

1. Merge Sort

// Code Part
#include<bits/stdc++.h>
using namespace std;
void merge(vector<int> &arr, int start, int mid, int end){
vector<int> temp(end-start+1);
int left = start;
int right = mid+1;
int index = 0;

while(left <= mid && right <= end){


if(arr[left] <= arr[right]){
temp[index] = arr[left];
index++;
left++;
}else{
temp[index] = arr[right];
index++;
right++;
}
}
while(left <= mid){
temp[index] = arr[left];
index++;
left++;
}
while(right <= end){
temp[index] = arr[right];
index++;
right++;
}
index = 0;
// put this value to arr
while(start <= end){
arr[start] = temp[index];
start++, index++;
}

void mergeSort(vector<int> &arr, int start , int end){


if(start == end){
return;
}
int mid = start + (end - start)/2;
// left side
mergeSort(arr,start, mid);
// right side
mergeSort(arr, mid+1, end);

merge(arr, start, mid, end);


}

int main(){

vector<int> arr = {10 , 4, 8, 2, 9};


for(int i = 0; i < 5; i++){
cout << arr[i] << " ";
}cout<< endl;
mergeSort(arr,0,4);
for(int i = 0; i < 5; i++){
cout << arr[i] << " ";
}
}

// Output
2. Quick Sort

// Code Part
#include<bits/stdc++.h>
using namespace std;
int partition(vector<int> &arr, int start, int end){
int pos = start;
for(int i = start; i <= end; i++){
if(arr[i] <= arr[end]){
swap(arr[pos], arr[i]);
pos++;
}
}
return pos-1;
}

void quickSort(vector<int> &arr, int start, int end){

if( start >= end)


return ;

int pivot = partition(arr, start, end);

// left side
quickSort(arr, start, pivot-1);
quickSort(arr, pivot+1, end);

}
int main()
{
vector<int> arr = {2, 5, 3, 4, 1, 3};
quickSort(arr, 0, 5);
for(int i = 0; i < 6; i++){
cout << arr[i] << " ";
}cout << endl;

return 0;
}
// Output
3. Bubble sort

// Code Part
void bubbleSort(int arr[], int n){
for(int i = 0; i < n-1; i++){
bool swapped = false;
for(int j = 1; j < n - i; j++){
if(arr[j] < arr[j-1]){
swapped = true;
swap(arr[j], arr[j-1]);
}
}
if(swapped == false){
break;
}
}
}
int main(){
int arr[] = {5, 2, 4,1,9, 10, 3};
cout << "Before Sorting: ";
printArr(arr, 7);
bubbleSort(arr, 7);
cout << "After Sorting: ";
printArr(arr, 7);

// Output
3. Selection sort

// Code Part
int getMaxIndex(int arr[], int start, int end){
int maxIndex = 0;
for(int i = start; i <= end; i++){
if(arr[i] > arr[maxIndex]){
maxIndex = i;
}
}
return maxIndex;
}
void selectionSort(int arr[], int n){
for(int i = 0; i < n-1; i++){
int last = n-1-i;
int maxIndex = getMaxIndex(arr, 0, last);
swap(arr[last], arr[maxIndex]);
}
}

int main(){
int arr[] = {5, 2, 4,1,9, 10, 3};
cout << "Before Sorting: ";
printArr(arr, 7);
selectionSort(arr,7);
cout << "After Sorting: ";
printArr(arr, 7);

// Output
5. Insertion sort

// Code Part
void insertionSort(int arr[], int n){
for(int i = 0; i < n-1; i++){
for(int j = i+1; j > 0; j--){
if(arr[j] < arr[j-1]){
swap(arr[j], arr[j-1]);
}else{
break;
}
}
}
}

int main(){
int arr[] = {5, 2, 4,1,9, 10, 3};
cout << "Before Sorting: ";
printArr(arr, 7);
insertionSort(arr, 7);
cout << "After Sorting: ";
printArr(arr, 7);

// Output

You might also like