Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 98ba1d0

Browse files
Add files via upload
1 parent 940b32c commit 98ba1d0

File tree

3 files changed

+185
-0
lines changed

3 files changed

+185
-0
lines changed

‎Devide and Conquer/Max_Min.cpp‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#include<iostream>
2+
using namespace std;
3+
4+
void max_min(int arr[],int low,int high,int &max1,int &min1)
5+
{
6+
if(low==high)
7+
{
8+
max1=arr[low];
9+
min1=arr[high];
10+
}
11+
else if (low==high-1)
12+
{
13+
if (arr[low]>arr[high])
14+
{
15+
max1=arr[low];
16+
min1=arr[high];
17+
}
18+
else
19+
{
20+
max1=arr[high];
21+
min1=arr[low];
22+
}
23+
}
24+
else
25+
{
26+
int mid=(low+high)/2;
27+
int max2,min2;
28+
29+
max_min(arr,low,mid,max1,min1);
30+
max_min(arr,mid+1,high,max2,min2);
31+
32+
if (max1<max2)
33+
max1=max2;
34+
if (min1>min2)
35+
min1=min2;
36+
}
37+
}
38+
39+
int main ()
40+
{
41+
int n;
42+
int max1,min1;
43+
cout<<"Enter the length of the array: ";
44+
cin>>n;
45+
int arr[n+1];
46+
cout<<"Enter the array: ";
47+
for (int i=0;i<n;i++)
48+
cin>>arr[i];
49+
50+
max_min(arr,0,n-1,max1,min1);
51+
52+
cout<<"Minimum element in the array: "<<min1<<endl;
53+
cout<<"Maximum element in the array: "<<max1<<endl;
54+
return 0;
55+
}
56+

‎Devide and Conquer/Merge_Sort.cpp‎

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
void Merge (int arr[],int low,int mid,int high)
5+
{
6+
int i=low;
7+
int j=mid+1;
8+
int k=low;
9+
int Sort[high];
10+
while (i<=mid && j<=high)
11+
{
12+
if (arr[i]<arr[j])
13+
{
14+
Sort[k]=arr[i];
15+
i++;
16+
}
17+
else
18+
{
19+
Sort[k]=arr[j];
20+
j++;
21+
}
22+
k++;
23+
}
24+
while (i<=mid)
25+
{
26+
Sort[k]=arr[i];
27+
i++;
28+
k++;
29+
}
30+
while (j<=high)
31+
{
32+
Sort[k]=arr[j];
33+
j++;
34+
k++;
35+
}
36+
for (k=low;k<=high;k++)
37+
arr[k]=Sort[k];
38+
}
39+
40+
void merge_sort (int arr[],int low,int high)
41+
{
42+
if (low<high)
43+
{
44+
int mid=(low+high)/2;
45+
merge_sort(arr,low,mid);
46+
merge_sort(arr,mid+1,high);
47+
Merge (arr,low,mid,high);
48+
}
49+
}
50+
51+
void printArray(int arr[], int size)
52+
{
53+
int i;
54+
cout<<"Sorted array: ";
55+
for (i=0;i<size;i++)
56+
cout<<arr[i]<<" ";
57+
cout<<endl;
58+
}
59+
60+
int main()
61+
{
62+
int arr[30],n;
63+
cout<<"Enter the size of array: ";
64+
cin>>n;
65+
cout<<"Enter the array:"<<endl;
66+
for (int i=1;i<=n;i++)
67+
cin>>arr[i];
68+
merge_sort(arr,0,n);
69+
printArray(arr,n);
70+
return 0;
71+
}
72+

‎Devide and Conquer/Quick_Sort.cpp‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#include<iostream>
2+
using namespace std;
3+
4+
void Swap(int &a,int &b)
5+
{
6+
int temp=a;
7+
a=b;
8+
b=temp;
9+
}
10+
11+
int Partition (int arr[],int low,int high)
12+
{
13+
int pivot=arr[low];
14+
int left=low;
15+
int right=high;
16+
while (left<right)
17+
{
18+
while (arr[left]<=pivot)
19+
left++;
20+
while (arr[right]>pivot)
21+
right--;
22+
if (left<right)
23+
Swap(arr[left],arr[right]);
24+
}
25+
Swap(arr[low],arr[right]);
26+
return right;
27+
}
28+
29+
int quick_sort(int arr[],int low,int high)
30+
{
31+
int key;
32+
if(low<=high)
33+
{
34+
key=Partition(arr,low,high);
35+
quick_sort(arr,low,key-1);
36+
quick_sort(arr,key+1,high);
37+
}
38+
}
39+
int main()
40+
{
41+
int n;
42+
cout<<"Enter the size of array: ";
43+
cin>>n;
44+
int a[n];
45+
cout<<"Enter the array: ";
46+
for(int i=0;i<n;++i)
47+
cin>>a[i];
48+
quick_sort(a,0,n-1);
49+
cout<<"The sorted array is"<<endl;
50+
for(int i=0;i<n;++i)
51+
cout<<a[i]<<" ";
52+
cout<<endl;
53+
return 0;
54+
}
55+
56+
57+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /