Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
16 views

Algorithm Sorting

The document discusses several sorting algorithms including bubble sort, insertion sort, selection sort, quick sort, merge sort, heap sort, and three way merge sort. Code snippets are provided for each algorithm's implementation.

Uploaded by

zakia.syeed51
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Algorithm Sorting

The document discusses several sorting algorithms including bubble sort, insertion sort, selection sort, quick sort, merge sort, heap sort, and three way merge sort. Code snippets are provided for each algorithm's implementation.

Uploaded by

zakia.syeed51
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Bubble Sort

Code:

#include<iostream> flag = 0;
#include<vector> for (j=0; j<n-1-i; j++)
using namespace std; {
int main() if (a[j]>a[j+1])
{ {
int i, j, flag, n, temp, element; temp = a[j];
cout << "Enter a number : "; a[j] = a[j+1];
cin >> n; a[j+1] = temp;
vector <int> a; flag = 1;
for (int i=0; i<n; i++) }
{ }
cin >> element; if (flag == 0)
a.push_back(element); break;
} }
cout << "Elements : "; cout << "\nSorted : ";
for (int i=0; i<a.size(); i++) for (int i=0; i<a.size(); i++)
{ {
cout << a[i] << " "; cout << a[i] << " ";
} }
for (i=0; i<n-1; i++) return 0;
{ }

Output:
Insertion sort

Code:

#include<bits/stdc++.h> for (i=1; i<n; i++)


using namespace std; {
int main() temp = a[i];
{ j = i-1;
int i, j, n, element, temp; while (j>=0 && a[j]>temp)
cout << "enter size: "; {
cin >> n; a[j+1] = a[j];
vector <int> a; j--;
for (i=0; i<n; i++) }
{ a[j+1] = temp;
cin >> element; }
a.push_back(element); cout << "Sorted Elements: ";
} for (i=0; i<a.size(); i++)
cout << "Elements: "; {
for (i=0; i<a.size(); i++) cout << a[i] << " ";
{ }
cout << a[i] << " ";
} return 0;
cout << endl; }

Output:
Selection Sort

Code:

#include<bits/stdc++.h> {
using namespace std; if(a[j]<a[mini])
int main() {
{ mini = j;
int i, j, n, element, temp, mini; }
cout << "Enter a number: "; }
cin >> n; if(mini != i)
vector <int> a; {
for(i=0; i<n; i++) temp = a[i];
{ a[i] = a[mini];
cin >> element; a[mini] = temp;
a.push_back(element); }
} }
cout << "Elements: "; cout << endl;
for(i=0; i<a.size(); i++) cout << "Sorted Elements: ";
{ for(i=0; i<a.size(); i++)
cout << a[i] << " "; {
} cout << a[i] << " ";
for(i=0; i<n-1; i++) }
{ return 0;
mini = i; }
for (j=i+1; j<=n-1; j++)

Output:
Quick Sort

Code:

#include<bits/stdc++.h> {
#include<vector> int idx = partition(a, l, r);
using namespace std; quickSort(a, l, idx - 1);
int partition(int a[], int l, int r) quickSort(a, idx + 1, r);
{ }
int i, j, pivot; }
pivot = a[l];
i = l; void printArray(int a[], int size)
j = r; {
while (i < j) int i, n;
{ for (i=0; i<n; i++)
while(a[i] <= pivot) {
{ cout << a[i] << " ";
i++; }
} cout << endl;
while(a[j] > pivot) }
{ int main()
j--; {
} int i, n, element;
if(i < j) cout << "Enter a number : ";
{ cin >> n;
swap(a[i], a[j]); int a[100];
} for (i=0; i<n; i++)
} {
swap(a[l], a[j]); cin >> a[i];
return j; }
} quickSort(a, 0, n - 1);
void quickSort(int a[], int l, int r) cout << "Sorted array: \n";
{ printArray(a, n);
if (l < r) return 0;
}

Output:
Two Way Merge Sort

Code:

#include<bits/stdc++.h> while (j < n2)


using namespace std ; {
void merge(int arr[], int l, int m, int r) arr[k] = R[j];
{ j++;
int i, j, k; k++;
int n1 = m - l + 1; }
int n2 = r - m; }
int L[n1], R[n2]; void MergeSort(int a[], int lb, int ub)
for (i = 0; i < n1; i++) {
L[i] = arr[l + i]; if (lb < ub)
for (j = 0; j < n2; j++) {
R[j] = arr[m + 1 + j]; int mid = (lb + ub) / 2;
i = 0; MergeSort(a, lb, mid);
j = 0; MergeSort(a, mid + 1, ub);
k = l; merge(a, lb, mid, ub);
while (i < n1 && j < n2) }
{ }
if (L[i] <= R[j]) int main()
{ {
arr[k] = L[i]; int n ;
i++; cout<<"Enter total number of elements : " ;
} cin>> n ;
else int a[n];
{ cout<<"Enter elements : " ;
arr[k] = R[j]; for(int i=0 ; i<n ; i++)
j++; cin>>a[i];
} cout<<endl ;
k++; MergeSort(a, 0, n-1) ;
} cout<<"2 way Merge Sorted Elements : ";
while (i < n1) for(int i=0 ; i<n ; i++)
{ cout<<a[i]<<" " ;
arr[k] = L[i]; cout<<endl ;
i++; return 0 ;
k++; }
}

Output:
Heap Sort

Code:

#include<bits/stdc++.h> {
using namespace std ; swap(arr[0], arr[i]);
void heapify(int arr[], int N, int i) heapify(arr, i, 0);
{ }
int largest = i; }
int left = 2 * i + 1; int main()
int right = 2 * i + 2; {
if (left < N && arr[left] > arr[largest]) int n ;
largest = left; cout<<"Enter total number of elements : " ;
if (right < N && arr[right] > arr[largest]) cin>> n ;
largest = right; int a[n];
cout<<"Enter elements : " ;
if (largest != i) for(int i=0 ; i<n ; i++)
{ cin>>a[i];
swap(arr[i], arr[largest]); cout<<endl ;
heapify(arr, N, largest); heapSort(a, n);
} cout<<"Heap Sorted Elements : ";
} for(int i=0 ; i<n ; i++)
void heapSort(int arr[], int N) cout<<a[i]<<" " ;
{ cout<<endl ;
for (int i = N / 2 - 1; i >= 0; i--) return 0 ;
heapify(arr, N, i); }
for (int i = N - 1; i >= 0; i--)

Output:
Three Way Merge Sort

Code:

#include <iostream> }
#include <algorithm> }
using namespace std; while (i < mid1) {
void merge1(int gArray1[], int low1, int mid1, destArray1[l++] = gArray1[i++];
int mid2, int high1, int destArray1[]) { }
int i = low1, j = mid1, k = mid2, l = low1; while (j < mid2) {
while ((i < mid1) && (j < mid2) && (k < destArray1[l++] = gArray1[j++];
high1)) { }
if (gArray1[i] < gArray1[j]) { while (k < high1) {
if (gArray1[i] < gArray1[k]) { destArray1[l++] = gArray1[k++];
destArray1[l++] = gArray1[i++]; }
} else { }
destArray1[l++] = gArray1[k++]; void mergeSort3WayRec(int gArray1[], int
} low1, int high1, int destArray1[]) {
} else { if (high1 - low1 < 2)
if (gArray1[j] < gArray1[k]) { return;
destArray1[l++] = gArray1[j++]; int mid1 = low1 + ((high1 - low1) / 3);
} else { int mid2 = low1 + 2 * ((high1 - low1) / 3) +
destArray1[l++] = gArray1[k++]; 1;
} mergeSort3WayRec(destArray1, low1,
} mid1, gArray1);
} mergeSort3WayRec(destArray1, mid1,
while ((i < mid1) && (j < mid2)) { mid2, gArray1);
if (gArray1[i] < gArray1[j]) { mergeSort3WayRec(destArray1, mid2,
destArray1[l++] = gArray1[i++]; high1, gArray1);
} else { merge1(destArray1, low1, mid1, mid2,
destArray1[l++] = gArray1[j++]; high1, gArray1);
} }
} void mergeSort3Way(int gArray1[], int n1) {
while ((j < mid2) && (k < high1)) { if (n1 == 0)
if (gArray1[j] < gArray1[k]) { return;
destArray1[l++] = gArray1[j++]; int fArray1[n1];
} else { for (int i = 0; i < n1; i++)
destArray1[l++] = gArray1[k++]; fArray1[i] = gArray1[i];
} mergeSort3WayRec(fArray1, 0, n1,
} gArray1);
while ((i < mid1) && (k < high1)) { for (int i = 0; i < n1; i++)
if (gArray1[i] < gArray1[k]) { gArray1[i] = fArray1[i];
destArray1[l++] = gArray1[i++]; }
} else { int main() {
destArray1[l++] = gArray1[k++]; int n = 0;
cout << "Enter the number of elements: ";
cin >> n;
int data1[n];
cout << "Enter " << n << " elements: ";
for (int i = 0; i < n; i++)
cin >> data1[i];
mergeSort3Way(data1, n);
cout << "After 3-way merge sort: ";
for (int i = 0; i < n; i++)
cout << data1[i] << " ";
cout << endl;

return 0;
}

Output:
Randomized Quick Sort

Code:

#include <iostream> void RandomizedQuickSort(vector<int>& arr,


#include <vector> int low, int high) {
#include <cstdlib> if (low < high) {
#include <ctime> int pi = RandomPartition(arr, low, high);
using namespace std; RandomizedQuickSort(arr, low, pi - 1);
void swap(int& a, int& b) { RandomizedQuickSort(arr, pi + 1, high);
int temp = a; }
a = b; }
b = temp;
} int main() {
int RandomPartition(vector<int>& arr, int int n;
low, int high) { cout << "Enter the size of the array: ";
srand(time(NULL)); cin >> n;
int randomIndex = low + rand() % (high - vector<int> arr(n);
low + 1); cout << "Enter the elements of the array: ";
swap(arr[randomIndex], arr[high]); for (int i = 0; i < n; i++) {
int pivot = arr[high]; cin >> arr[i];
int i = low - 1; }
for (int j = low; j <= high - 1; j++) { RandomizedQuickSort(arr, 0, n - 1);
if (arr[j] <= pivot) { cout << "After Randomized QuickSort:\n";
i++; for (int i = 0; i < n; i++) {
swap(arr[i], arr[j]); cout << arr[i] << " ";
} }
} cout << endl;
swap(arr[i + 1], arr[high]); return 0;
return (i + 1); }
}

Output:

You might also like