-
Notifications
You must be signed in to change notification settings - Fork 6.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #224 from chaitanyatekane/master
QuickSort and Factorial CPP Code
- Loading branch information
Showing
3 changed files
with
84 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 53 additions & 55 deletions
108
Program's_Contributed_By_Contributors/C++_Programs/Quicksort.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,53 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
void swap(int* a, int* b) | ||
{ | ||
int t = *a; | ||
*a = *b; | ||
*b = t; | ||
} | ||
|
||
int partition (int arr[], int low, int high) | ||
{ | ||
int pivot = arr[high]; | ||
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); | ||
} | ||
|
||
void quickSort(int arr[], int low, int high) | ||
{ | ||
if (low < high) | ||
{ | ||
int pi = partition(arr, low, high); | ||
|
||
quickSort(arr, low, pi - 1); | ||
quickSort(arr, pi + 1, high); | ||
} | ||
} | ||
|
||
void printArray(int arr[], int size) | ||
{ | ||
int i; | ||
for (i = 0; i < size; i++) | ||
cout << arr[i] << " "; | ||
cout << endl; | ||
} | ||
|
||
int main() | ||
{ | ||
int arr[] = {10, 7, 8, 9, 1, 5}; | ||
int n = sizeof(arr) / sizeof(arr[0]); | ||
quickSort(arr, 0, n - 1); | ||
cout << "Sorted array: \n"; | ||
printArray(arr, n); | ||
return 0; | ||
} | ||
#include<bits/stdc++.h> | ||
using namespace std; | ||
int partition(int *a,int start,int end) | ||
{ | ||
int pivot=a[end]; | ||
|
||
int P_index=start; | ||
int i,t; | ||
|
||
for(i=start;i<end;i++) | ||
{ | ||
if(a[i]<=pivot) | ||
{ | ||
t=a[i]; | ||
a[i]=a[P_index]; | ||
a[P_index]=t; | ||
P_index++; | ||
} | ||
} | ||
t=a[end]; | ||
a[end]=a[P_index]; | ||
a[P_index]=t; | ||
|
||
return P_index; | ||
} | ||
void Quicksort(int *a,int start,int end) | ||
{ | ||
if(start<end) | ||
{ | ||
int P_index=partition(a,start,end); | ||
Quicksort(a,start,P_index-1); | ||
Quicksort(a,P_index+1,end); | ||
} | ||
} | ||
int main() | ||
{ | ||
int n; | ||
cout<<"Enter number of elements: "; | ||
cin>>n; | ||
int a[n]; | ||
cout<<"Enter the array elements:\n"; | ||
for(int i=0;i<n;i++) | ||
{ | ||
cin>>a[i]; | ||
} | ||
Quicksort(a,0,n-1); | ||
cout<<"After Quick Sort the array is:\n"; | ||
for(int i=0;i<n;i++) | ||
{ | ||
cout<<a[i]<<" "; | ||
} | ||
return 0; | ||
} |
28 changes: 17 additions & 11 deletions
28
Program's_Contributed_By_Contributors/C++_Programs/factorial.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,21 @@ | ||
#include <iostream> | ||
|
||
int factorial(int n) { | ||
if(n == 0) { | ||
return 1; | ||
} | ||
if(n > 0) { | ||
return n * factorial(n - 1); | ||
} | ||
return 0; | ||
} | ||
using namespace std; | ||
|
||
int main() { | ||
std::cout << factorial(3); | ||
int n; | ||
long double factorial = 1.0; | ||
|
||
cout << "Enter a positive integer: "; | ||
cin >> n; | ||
|
||
if (n < 0) | ||
cout << "Error! Factorial of a negative number doesn't exist."; | ||
else { | ||
for(int i = 1; i <= n; ++i) { | ||
factorial *= i; | ||
} | ||
cout << "Factorial of " << n << " = " << factorial; | ||
} | ||
|
||
return 0; | ||
} |