Skip to content

Commit 9d863df

Browse files
committed
1 parent 80b5f18 commit 9d863df

File tree

5 files changed

+170
-160
lines changed

5 files changed

+170
-160
lines changed

C++/Algorithms/SortingAlgorithms/heapSort.cpp

+36-33
Original file line numberDiff line numberDiff line change
@@ -5,59 +5,62 @@
55

66
/* Time Complexity : O(nlogn) */
77

8-
#include<iostream>
8+
#include <iostream>
99
using namespace std;
1010
#define MAX 100000
1111

12-
void adjust(int a[] , int n ,int i){ // to heapify the root
13-
while(2*i+1<=n){
14-
int j = 2*i+1;
15-
if(j+1<=n && a[j+1]>a[j])
16-
j = j+1;
17-
if(a[i] >= a[j])
12+
void adjust(int a[], int n, int i)
13+
{ // to heapify the root
14+
while (2 * i + 1 <= n)
15+
{
16+
int j = 2 * i + 1;
17+
if (j + 1 <= n && a[j + 1] > a[j])
18+
j = j + 1;
19+
if (a[i] >= a[j])
1820
break;
19-
else{
21+
else
22+
{
2023
int temp = a[i];
2124
a[i] = a[j];
2225
a[j] = temp;
2326
i = j;
2427
}
2528
}
2629
}
27-
void heapsort(int a[] , int n){
28-
for(int i = n/2 - 1;i>=0 ;i--){ // build heap
29-
adjust(a,n-1,i);
30+
void heapsort(int a[], int n)
31+
{
32+
for (int i = n / 2 - 1; i >= 0; i--)
33+
{ // build heap
34+
adjust(a, n - 1, i);
3035
}
3136

32-
while(n>0){ // move current root to end
37+
while (n > 0)
38+
{ // move current root to end
3339
int t = 0;
34-
t = a[0];
35-
a[0] = a[n-1];
36-
a[n-1]=t;
40+
t = a[0];
41+
a[0] = a[n - 1];
42+
a[n - 1] = t;
3743
n--;
38-
adjust(a,n-1,0); // heapify the newly formed root
44+
adjust(a, n - 1, 0); // heapify the newly formed root
3945
}
40-
41-
4246
}
4347

44-
45-
int main(){
48+
int main()
49+
{
4650
int n;
47-
cout<<"Enter the number of elments in array : ";
48-
cin>>n;
51+
cout << "Enter the number of elments in array : ";
52+
cin >> n;
4953
int a[MAX];
50-
cout<<"Enter the elements in heap"<<endl;
51-
for(int i=0;i<n;i++){
52-
cin>>a[i];
54+
cout << "Enter the elements in heap" << endl;
55+
for (int i = 0; i < n; i++)
56+
{
57+
cin >> a[i];
5358
}
54-
heapsort(a,n);
55-
cout<<"Sorted array is using heapsort is"<<endl;
56-
for(int i = 0;i<n;i++){
57-
cout<<a[i]<<" ";
59+
heapsort(a, n);
60+
cout << "Sorted array is using heapsort is" << endl;
61+
for (int i = 0; i < n; i++)
62+
{
63+
cout << a[i] << " ";
5864
}
59-
cout<<endl;
60-
65+
cout << endl;
6166
}
62-
63-

C++/Algorithms/SortingAlgorithms/mergeSort.cpp

+65-58
Original file line numberDiff line numberDiff line change
@@ -5,76 +5,83 @@
55
/* Time complexity : O(nlogn) in all cases
66
Space complexity: O(n) */
77

8-
#include<bits/stdc++.h>
8+
#include <bits/stdc++.h>
99
using namespace std;
1010
#define MAX 100000
1111

12-
void merge(int arr[],int l,int m,int r) // fn for merging the elements in sorted manner.
12+
void merge(int arr[], int l, int m, int r) // fn for merging the elements in sorted manner.
1313
{
14-
int i, j, k;
15-
int n1 = m - l + 1; // elements in LHS
16-
int n2 = r - m; // elements in RHS
17-
14+
int i, j, k;
15+
int n1 = m - l + 1; // elements in LHS
16+
int n2 = r - m; // elements in RHS
17+
1818
int L[n1], R[n2]; // temporary arrays
1919

20-
for (i = 0; i < n1; i++)
21-
L[i] = arr[l + i];
22-
for (j = 0; j < n2; j++)
23-
R[j] = arr[m + 1 + j];
24-
25-
i = 0;
26-
j = 0;
27-
k = l;
28-
while (i < n1 && j < n2) {
29-
if (L[i] <= R[j]) {
30-
arr[k] = L[i];
31-
i++;
32-
}
33-
else {
34-
arr[k] = R[j];
35-
j++;
36-
}
37-
k++;
38-
}
20+
for (i = 0; i < n1; i++)
21+
L[i] = arr[l + i];
22+
for (j = 0; j < n2; j++)
23+
R[j] = arr[m + 1 + j];
3924

40-
while (i < n1) {
41-
arr[k] = L[i];
42-
i++;
43-
k++;
44-
}
25+
i = 0;
26+
j = 0;
27+
k = l;
28+
while (i < n1 && j < n2)
29+
{
30+
if (L[i] <= R[j])
31+
{
32+
arr[k] = L[i];
33+
i++;
34+
}
35+
else
36+
{
37+
arr[k] = R[j];
38+
j++;
39+
}
40+
k++;
41+
}
4542

46-
while (j < n2) {
47-
arr[k] = R[j];
48-
j++;
49-
k++;
50-
}
43+
while (i < n1)
44+
{
45+
arr[k] = L[i];
46+
i++;
47+
k++;
48+
}
49+
50+
while (j < n2)
51+
{
52+
arr[k] = R[j];
53+
j++;
54+
k++;
55+
}
5156
}
52-
void mergeSort(int *a,int low,int high) //recursive calls for dividing array
57+
void mergeSort(int *a, int low, int high) //recursive calls for dividing array
5358
{
54-
if(low<high){
55-
int mid= (low+high)/2;
56-
mergeSort(a,low,mid);
57-
mergeSort(a,mid+1,high);
58-
merge(a,low,mid,high);
59+
if (low < high)
60+
{
61+
int mid = (low + high) / 2;
62+
mergeSort(a, low, mid);
63+
mergeSort(a, mid + 1, high);
64+
merge(a, low, mid, high);
5965
}
6066
}
6167

62-
63-
int main(){
64-
int a[MAX];
65-
int n,num;
66-
cout<<"Enter the number of elements in the array: ";
67-
cin>>n;
68-
cout<<"Enter the elements " ;
69-
for(int i=0;i<n;i++){
70-
cin>>num;
71-
a[i]=num;
72-
}
73-
mergeSort(a,0,n-1);
74-
cout<<"The sorted elements are : ";
75-
for(int i=0;i<n;i++)
68+
int main()
7669
{
77-
cout<<a[i]<<" ";
78-
}
79-
return 0;
70+
int a[MAX];
71+
int n, num;
72+
cout << "Enter the number of elements in the array: ";
73+
cin >> n;
74+
cout << "Enter the elements ";
75+
for (int i = 0; i < n; i++)
76+
{
77+
cin >> num;
78+
a[i] = num;
79+
}
80+
mergeSort(a, 0, n - 1);
81+
cout << "The sorted elements are : ";
82+
for (int i = 0; i < n; i++)
83+
{
84+
cout << a[i] << " ";
85+
}
86+
return 0;
8087
}

C++/Algorithms/SortingAlgorithms/quickSort.cpp

+34-34
Original file line numberDiff line numberDiff line change
@@ -5,58 +5,58 @@
55

66
/* Time complexity: Worst case : O(n^2), Average case: O(nlogn), Best case: O(nlogn) */
77

8-
#include<bits/stdc++.h>
8+
#include <bits/stdc++.h>
99
using namespace std;
1010
#define MAX 100000
1111

12-
void swap(int* a,int* b) //for swapping elements
12+
void swap(int *a, int *b) //for swapping elements
1313
{
14-
int t=*a;
15-
*a=*b;
16-
*b=t;
14+
int t = *a;
15+
*a = *b;
16+
*b = t;
1717
}
1818

19-
int partition(int arr[],int low,int high) //for chosing partitioning around pivot
19+
int partition(int arr[], int low, int high) //for chosing partitioning around pivot
2020
{
21-
int pivot=arr[high];
22-
int i=(low-1);
23-
for(int j=low;j<=high;j++)
21+
int pivot = arr[high];
22+
int i = (low - 1);
23+
for (int j = low; j <= high; j++)
2424
{
25-
if(arr[j]<pivot)
25+
if (arr[j] < pivot)
2626
{
2727
i++;
28-
swap(&arr[i],&arr[j]);
28+
swap(&arr[i], &arr[j]);
2929
}
3030
}
31-
swap(&arr[i+1],&arr[high]);
32-
return (i+1);
31+
swap(&arr[i + 1], &arr[high]);
32+
return (i + 1);
3333
}
3434

35-
void quicksort(int arr[],int low,int high) // recursive calls for partitioning.
35+
void quicksort(int arr[], int low, int high) // recursive calls for partitioning.
3636
{
37-
if (low<high)
37+
if (low < high)
3838
{
39-
int pi=partition(arr,low,high);
40-
quicksort(arr,low,pi-1);
41-
quicksort(arr,pi+1,high);
39+
int pi = partition(arr, low, high);
40+
quicksort(arr, low, pi - 1);
41+
quicksort(arr, pi + 1, high);
4242
}
4343
}
4444

4545
int main()
46-
{
47-
int n,num,x;
48-
int arr[MAX];
49-
cout<<"Enter the number of elements in the array : ";
50-
cin>>n;
51-
cout<<"Enter the elements : ";
52-
for(int i=0;i<n;i++)
53-
{
54-
cin>>num;
55-
arr[i]=num;
56-
}
57-
quicksort(arr,0,n-1);
58-
cout<<"Array after sorting: ";
59-
for(int i=0;i<n;i++)
60-
cout<<arr[i]<<" ";
61-
return 0;
46+
{
47+
int n, num, x;
48+
int arr[MAX];
49+
cout << "Enter the number of elements in the array : ";
50+
cin >> n;
51+
cout << "Enter the elements : ";
52+
for (int i = 0; i < n; i++)
53+
{
54+
cin >> num;
55+
arr[i] = num;
56+
}
57+
quicksort(arr, 0, n - 1);
58+
cout << "Array after sorting: ";
59+
for (int i = 0; i < n; i++)
60+
cout << arr[i] << " ";
61+
return 0;
6262
}

0 commit comments

Comments
 (0)