Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

All Sort

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

INSERTION SORT

#include <iostream>

#include <vector>

#include<bits/stdc++.h>

using namespace std;

void Insertion (int a[], int n)

int pos,i;

int x;

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

x = a[i];

pos = i -1;

while((pos >= 0) && (a[pos]>x))

a[pos + 1] =a[pos];

pos--;

a[pos+1] = x;

int main()

double s, e , t;

s = clock();

int n;

cin>> n;

int *a = new int [n];

ifstream exam;
exam.open("a.txt");

for (int i =0; i< n; i++)

exam >> a[i];

exam.close();

Insertion(a,n);

for (int i = 0; i<n; i++)

cout<<a[i]<< " ";

e = clock();

t = (e - s)/CLOCKS_PER_SEC;

cout<<"time: "<< t;

return 0;

QUICK SORT

#include <iostream>

#include<bits/stdc++.h>

using namespace std;

void hoanvi(int &a, int &b)

int temp = a;

a = b;

b = temp;

void Quicksort(int a[], int left, int right)

int i, j , x;

x = a[(left + right)/2];

i = left;

j = right;
while (i < j)

while (a[i] < x)

i++;

while (a[j] > x)

j--;

if(i<= j)

hoanvi(a[i], a[j]);

i++;

j--;

if (left<j)

Quicksort(a, left, j);

if (i < right )

Quicksort (a, i , right);

int main()

double s,e,t;

s = clock();

int n;

cin>> n;

int *a = new int [n];

ifstream exam ;

exam.open ("a.txt");

for (int i =0; i< n; i++)

exam >> a[i];


exam.close();

Quicksort(a,0, n -1);

e = clock();

t = (e - s)/CLOCKS_PER_SEC;

cout<<"Time: "<< t;

return 0;

HEAP SORT

#include <stdio.h>

#include<bits/stdc++.h>

using namespace std;

void swap(int* a, int* b)

int temp = *a;

*a = *b;

*b = temp;

void heapify(int arr[], int N, int i)

int largest = i;

int left = 2 * i + 1;

int right = 2 * i + 2;

if (left < N && arr[left] > arr[largest])

largest = left;

if (right < N && arr[right] > arr[largest])

largest = right;
if (largest != i) {

swap(&arr[i], &arr[largest]);

heapify(arr, N, largest);

}}

void heapSort(int arr[], int N)

for (int i = N / 2 - 1; i >= 0; i--)

heapify(arr, N, i);

for (int i = N - 1; i >= 0; i--) {

swap(&arr[0], &arr[i]);

heapify(arr, i, 0);}

int main()

double s,e,t;

s = clock();

int n;

cin>> n;

int *a = new int [n];

ifstream exam;

exam.open("a.txt");

for (int i =0; i< n; i++)

exam >> a[i];

exam.close();

heapSort(arr, N);

e = clock();

t = (e - s)/CLOCKS_PER_SEC;

cout<<"Time: "<< t;

return 0;
}

MERGE SORT

#include <iostream>

#include<bits/stdc++.h>

using namespace std;

void Merge(int array[], int left, int mid,

int right)

int x = mid - left + 1;

int y = right - mid;

int *a = new int[x];

int *b = new int[y];

for (int i = 0; i < x; i++)

a[i] = array[left + i];

for (auto j = 0; j < y; j++)

b[j] = array[mid + 1 + j];

int o = 0;

int p = 0;

int k= left;

while (o < x&& p < y) {

if (a[o]<= b[p]) {

array[k]= a[o];

o++;

else {

array[k]= b[p];

p++;

}
k++;

while (o < x) {

array[k]= a[o];

o++;

k++;

while (p < y) {

array[k]= b[p];

p++;

k++;

delete[] a;

delete[] b;

void mergeSort(int array[], int beg, int en)

if (beg>= en)

return;

int mid = beg+ (en - beg) / 2;

mergeSort(array, beg, mid);

mergeSort(array, mid + 1, en);

Merge(array, beg, mid, en);

int main()

double s,e,t;
s = clock();

int n;

cin>> n;

int *a = new int [n];

ifstream exam;

exam.open("a.txt");

for (int i =0; i< n; i++)

exam >> a[i];

exam.close;

mergeSort(a,0, n -1);

e = clock();

t = (e - s)/CLOCKS_PER_SEC;

cout<<"Time: "<< t;

return 0;

You might also like