0% found this document useful (0 votes)
8 views5 pages

Sorting Algorithms in C++ Code Examples

The document provides implementations of five sorting algorithms: Selection Sort, Bubble Sort, Insertion Sort, Merge Sort, and Quick Sort, each with a brief description of their functionality. Each algorithm includes a main function to read an array of integers, sort it using the respective algorithm, and print the sorted array. The code snippets demonstrate the logic and structure of each sorting method in C++.

Uploaded by

Nisha sharma
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)
8 views5 pages

Sorting Algorithms in C++ Code Examples

The document provides implementations of five sorting algorithms: Selection Sort, Bubble Sort, Insertion Sort, Merge Sort, and Quick Sort, each with a brief description of their functionality. Each algorithm includes a main function to read an array of integers, sort it using the respective algorithm, and print the sorted array. The code snippets demonstrate the logic and structure of each sorting method in C++.

Uploaded by

Nisha sharma
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

​ ​ ​ SELECTION SORT-

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


//select minimun and swap
for(int i=0; i<=n-2; i++){
int min= i;
for(int j=i; j<=n-1; j++){
if (arr[j]< arr[min]){
min=j;
}

}
int temp= arr[min];
arr[min]= arr[i];
arr[i]= temp;
}
}
int main(){
int n;
cin>>n;
int arr[n];
for(int i=0; i<n; i++){
cin>>arr[i];
}
SELECTION_SORT(arr, n);
for(int i=0; i<n; i++){
cout<<arr[i]<<" ";
}
return 0;
}

​ ​ ​ ​ BUBBLE SORT-

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


//pushing the largest to the last.
//comparing the first with next. if greater than swap. then next with next
swap again if greater and so on

for(int i=n-1; i>=1; i--){


int didswap=0;
for(int j=0; j<=i-1; j++){
if(arr[j]>arr[j+1]){
int temp= arr[j+1];
arr[j+1]= arr[j];
arr[j]= temp;
didswap=1;
}
}
if(didswap=0){
break;
}
}
}
int main(){
int n;
cin>>n;
int arr[n];
for(int i=0; i<n; i++){
cin>>arr[i];
}
bubble_sort(arr, n);
for(int i=0; i<n; i++){
cout<<arr[i]<<" ";
}
return 0;
}

​ ​ ​ Insertion sort-

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


//takes the element and place it in its correct position
for(int i=0; i<=n-1; i++){
for(int j=i; j>0; j--){
if(arr[j]<arr[j-1]){
int temp= arr[j];
arr[j]=arr[j-1];
arr[j-1]= temp;
}
else{
break;
}
}
}
}
int main(){
int n;
cin>>n;
int arr[n];
for(int i=0; i<n; i++){
cin>>arr[i];
}
insertion_sort(arr, n);
for(int i=0; i<n; i++){
cout<<arr[i]<<" ";
}
return 0;
}

​ ​ ​ ​ MERGE SORT
#include <iostream>
using namespace std;

// Merge function: merges two sorted halves


void merge(int arr[], int low, int mid, int high) {
int n1 = mid - low + 1; // size of left sub-array
int n2 = high - mid; // size of right sub-array

int left[n1], right[n2];

// Copy data to temp arrays


for(int i = 0; i < n1; i++) left[i] = arr[low + i];
for(int i = 0; i < n2; i++) right[i] = arr[mid + 1 + i];

// Merge the temp arrays back


int i = 0, j = 0, k = low;
while(i < n1 && j < n2) {
if(left[i] <= right[j]) {
arr[k++] = left[i++];
} else {
arr[k++] = right[j++];
}
}

// Copy remaining elements of left[], if any


while(i < n1) {
arr[k++] = left[i++];
}

// Copy remaining elements of right[], if any


while(j < n2) {
arr[k++] = right[j++];
}
}

// Recursive merge sort


void mergeSort(int arr[], int low, int high) {
if(low >= high) return;

int mid = (low + high) / 2;

// Recursively sort first and second halves


mergeSort(arr, low, mid);
mergeSort(arr, mid + 1, high);
// Merge the sorted halves
merge(arr, low, mid, high);
}
// Driver code
int main() {
int arr[] = {38, 27, 43, 3, 9, 82, 10};
int n = sizeof(arr)/sizeof(arr[0]);

mergeSort(arr, 0, n - 1);

cout << "Sorted array: ";


for(int i = 0; i < n; i++) cout << arr[i] << " ";
return 0;
}
​ ​ ​ ​ QUICK SORT

//pick the pivot nd place it in its correct place int he sorted array
//smaller on the left, bigger on right
#include <iostream>
using namespace std;

// Partition function
int partition(int arr[], int low, int high) {
int pivot = arr[high]; // last element as pivot
int i = low - 1;

for(int j = low; j <= high - 1; j++) {


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

// Recursive Quick Sort


void quickSort(int arr[], int low, int high) {
if(low < high) {
int pivotIndex = partition(arr, low, high); // pivot place
quickSort(arr, low, pivotIndex - 1); // left side
quickSort(arr, pivotIndex + 1, high); // right side
}
}
int main() {
int arr[] = {5, 3, 8, 4, 2, 7, 1, 10};
int n = sizeof(arr)/sizeof(arr[0]);
quickSort(arr, 0, n - 1);
cout << "Sorted Array: ";
for(int i = 0; i < n; i++)
cout << arr[i] << " ";
return 0;
}

You might also like