mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-02-11 06:26:05 +08:00
Merge branch 'master' into fix-LL
This commit is contained in:
@@ -101,7 +101,7 @@ int main()
|
||||
cout<<"\n2. Delete";
|
||||
cout<<"\n3. Search";
|
||||
cout<<"\n4. Print";
|
||||
cout<<"\n5. Exit";
|
||||
cout<<"\n0. Exit";
|
||||
cout<<"\n\nEnter you choice : ";
|
||||
cin>>choice;
|
||||
switch (choice)
|
||||
@@ -115,8 +115,8 @@ int main()
|
||||
case 3 : cout<<"\nEnter the element to be searched : ";
|
||||
cin>>x;
|
||||
search(x); break;
|
||||
case 4 : show(); break;
|
||||
case 5 : exit(0);
|
||||
case 4 : show();
|
||||
cout<<"\n"; break;
|
||||
}
|
||||
}
|
||||
while(choice!=0);
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
|
||||
int stack[10];
|
||||
int top=0;
|
||||
int *stack;
|
||||
int top=0, size;
|
||||
|
||||
void push(int x)
|
||||
{
|
||||
if(top==10)
|
||||
if(top==size)
|
||||
{
|
||||
cout<<"\nOverflow";
|
||||
}
|
||||
@@ -42,6 +42,9 @@ void topmost()
|
||||
}
|
||||
int main()
|
||||
{
|
||||
cout<<"\nEnter Size of stack : ";
|
||||
cin>>size;
|
||||
stack = new int[size];
|
||||
int ch, x;
|
||||
do
|
||||
{
|
||||
|
||||
@@ -1,20 +1,24 @@
|
||||
#include<iostream>
|
||||
// This function convert decimal to binary number
|
||||
//
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
int main()
|
||||
{
|
||||
int number;
|
||||
cin>>number;
|
||||
int remainder,binary=0,var=1;
|
||||
|
||||
do{
|
||||
|
||||
remainder=number%2;
|
||||
number=number/2;
|
||||
binary=binary+(remainder*var);
|
||||
var=var*10;
|
||||
cout << "Enter a number:";
|
||||
cin >> number;
|
||||
int remainder, binary = 0, var = 1;
|
||||
|
||||
do {
|
||||
remainder = number % 2;
|
||||
number = number / 2;
|
||||
binary = binary + (remainder*var);
|
||||
var = var * 10;
|
||||
|
||||
}
|
||||
while(number>0);
|
||||
cout<<binary;
|
||||
} while (number>0);
|
||||
cout << "the binary is :";
|
||||
cout << binary;
|
||||
cout << endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -2,50 +2,25 @@
|
||||
|
||||
using namespace std;
|
||||
|
||||
void main(void)
|
||||
{
|
||||
int main(void){
|
||||
int valueToConvert = 0; //Holds user input
|
||||
int hexArray[8]; //Contains hex values backwards
|
||||
int i = 0; //counter
|
||||
int lValue = 0; //Last Value of Hex result
|
||||
char HexValues[] = "0123456789ABCDEF";
|
||||
|
||||
cout << "Enter a Decimal Value" << endl; //Displays request to stdout
|
||||
cin >> valueToConvert; //Stores value into valueToConvert via user input
|
||||
|
||||
while (valueToConvert > 0) //Dec to Hex Algorithm
|
||||
{
|
||||
lValue = valueToConvert % 16; //Gets remainder
|
||||
valueToConvert = valueToConvert / 16;
|
||||
hexArray[i] = lValue; //Stores converted values into an array
|
||||
i++;
|
||||
while (valueToConvert > 15){ //Dec to Hex Algorithm
|
||||
hexArray[i++] = valueToConvert % 16; //Gets remainder
|
||||
valueToConvert /= 16;
|
||||
}
|
||||
hexArray[i] = valueToConvert; //Gets last value
|
||||
|
||||
cout << "Hex Value: ";
|
||||
while (i > 0)
|
||||
{
|
||||
//Displays Hex Letters to stdout
|
||||
switch (hexArray[i - 1]) {
|
||||
case 10:
|
||||
cout << "A";
|
||||
break;
|
||||
case 11:
|
||||
cout << "B";
|
||||
break;
|
||||
case 12:
|
||||
cout << "C";
|
||||
break;
|
||||
case 13:
|
||||
cout << "D";
|
||||
break;
|
||||
case 14:
|
||||
cout << "E";
|
||||
break;
|
||||
case 15:
|
||||
cout << "F";
|
||||
break;
|
||||
default:
|
||||
cout << hexArray[i - 1]; //if not an int 10 - 15, displays int value
|
||||
}
|
||||
i--;
|
||||
}
|
||||
while (i >= 0)
|
||||
cout<<HexValues[hexArray[i--]];
|
||||
|
||||
cout << endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -30,7 +30,7 @@ int editDist(string str1, string str2, int m, int n) {
|
||||
|
||||
//If last characters are same then continue
|
||||
//for the rest of them.
|
||||
if(str1[m-1] == str2[n-2])
|
||||
if(str1[m-1] == str2[n-1])
|
||||
return editDist(str1, str2, m-1, n-1);
|
||||
|
||||
//If last not same, then 3 possibilities
|
||||
@@ -63,7 +63,7 @@ int editDistDP(string str1, string str2, int m, int n) {
|
||||
|
||||
//If character same. Recur for remaining
|
||||
else if(str1[i-1] == str2[j-1])
|
||||
dp[i][j] == dp[i-1][j-1];
|
||||
dp[i][j] = dp[i-1][j-1];
|
||||
|
||||
else
|
||||
dp[i][j] = 1 + min(dp[i][j-1],//Insert
|
||||
@@ -80,8 +80,8 @@ int main() {
|
||||
string str1 = "sunday";
|
||||
string str2 = "saturday";
|
||||
|
||||
cout << editDist(str1, str1, str1.length(), str2.length()) << endl;
|
||||
cout << editDistDP(str1, str1, str1.length(), str2.length()) << endl;
|
||||
cout << editDist(str1, str2, str1.length(), str2.length()) << endl;
|
||||
cout << editDistDP(str1, str2, str1.length(), str2.length()) << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
and this sum turns out to be 1 */
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
int main()
|
||||
{
|
||||
int n,k,s=0,d;
|
||||
@@ -23,4 +24,5 @@ int main()
|
||||
cout << n << " is a happy number" << endl;
|
||||
else
|
||||
cout << n << " is not a happy number" << endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -1,52 +1,63 @@
|
||||
#include<iostream>
|
||||
#include<stdlib.h>
|
||||
#include<string>
|
||||
#include<stdio.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
const int MAX = 100;
|
||||
#define MAX 100
|
||||
|
||||
// -------------- stack --------------
|
||||
|
||||
char stack[MAX];
|
||||
int top=0;
|
||||
int top = -1;
|
||||
|
||||
void push(char ch)
|
||||
{
|
||||
stack[top++]=ch;
|
||||
void push(char ch){
|
||||
stack[ ++top ] = ch;
|
||||
}
|
||||
|
||||
char pop()
|
||||
{
|
||||
return stack[--top];
|
||||
char pop(){
|
||||
return stack[ top-- ];
|
||||
}
|
||||
|
||||
// -------------- end stack -----------
|
||||
|
||||
int main()
|
||||
{
|
||||
char opening(char ch){
|
||||
switch(ch){
|
||||
case '}':
|
||||
return '{';
|
||||
case ']':
|
||||
return '[';
|
||||
case ')':
|
||||
return '(';
|
||||
case '>':
|
||||
return '<';
|
||||
}
|
||||
}
|
||||
|
||||
int main(){
|
||||
|
||||
string exp;
|
||||
int valid = 1, i = 0;
|
||||
cout<<"Enter The Expression : ";
|
||||
cin >> exp;
|
||||
for (int i = 0; i < exp.length(); i++)
|
||||
{
|
||||
if (exp[i]=='(' || exp[i]=='{' || exp[i]=='[' || exp[i]=='<')
|
||||
{
|
||||
|
||||
while (valid == 1 && i < exp.length()){
|
||||
if (exp[i] == '(' || exp[i] == '{' || exp[i] == '[' || exp[i] == '<'){
|
||||
push(exp[i]);
|
||||
}
|
||||
else if (exp[i]==')' || exp[i]=='}' || exp[i]==']' || exp[i]=='>')
|
||||
{
|
||||
pop();
|
||||
else if (top >= 0 && stack[top] == opening(exp[i])){
|
||||
pop();
|
||||
}
|
||||
else{
|
||||
valid = 0;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
|
||||
// makes sure the stack is empty after processsing (above)
|
||||
if(top==0)
|
||||
{
|
||||
cout<<"Correct Expression";
|
||||
if (valid == 1 && top == -1){
|
||||
cout<<"\nCorrect Expression";
|
||||
}
|
||||
else
|
||||
{
|
||||
else{
|
||||
cout<<"\nWrong Expression";
|
||||
}
|
||||
|
||||
|
||||
@@ -81,5 +81,5 @@ int main()
|
||||
cout << n << " th Fibonacci is \n";
|
||||
fib_Accurate(n);
|
||||
|
||||
return 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -8,7 +8,8 @@ How you can contribute? See this small guide.
|
||||
|
||||
* Use the directory structure of the repository.
|
||||
* Please describe your pull requests.
|
||||
* Don't use **bits/stdc++.h** because this is quite Linux specific and slow down the compiler process.
|
||||
* Put in comments in your code.
|
||||
* Avoid **struct** uses instead the **class** keyword.
|
||||
* Add some test cases in the main-function.
|
||||
* Don't use **bits/stdc++.h** because this is quite Linux specific and slows down the compiler process.
|
||||
* Put comments in your code.
|
||||
* Avoid **struct**. Instead use the **class** keyword.
|
||||
* Add some test cases in the main-function.
|
||||
* Can suggest any change in present algorithms(if needed).
|
||||
@@ -1,39 +1,55 @@
|
||||
//Bubble Sort
|
||||
|
||||
#include<iostream>
|
||||
#include<vector>
|
||||
using namespace std;
|
||||
|
||||
int main()
|
||||
{
|
||||
int n;
|
||||
short swap_check=0;
|
||||
cout << "Enter the amount of numbers to sort: ";
|
||||
cin >> n;
|
||||
int Array[n];
|
||||
cout<<"\nEnter any 6 Numbers for Unsorted Array : ";
|
||||
vector<int> numbers;
|
||||
cout << "Enter " << n << " numbers: ";
|
||||
int num;
|
||||
|
||||
//Input
|
||||
//Input
|
||||
for(int i=0; i<n; i++)
|
||||
{
|
||||
cin>>Array[i];
|
||||
cin >> num;
|
||||
numbers.push_back(num);
|
||||
}
|
||||
|
||||
//Bubble Sorting
|
||||
for(int i=0; i<n; i++)
|
||||
{
|
||||
for(int j=0; j<n-1; j++)
|
||||
swap_check=0;
|
||||
for(int j=0; j<n-1-i; j++)
|
||||
{
|
||||
if(Array[j]>Array[j+1])
|
||||
if(numbers[j]>numbers[j+1])
|
||||
{
|
||||
int temp=Array[j];
|
||||
Array[j]=Array[j+1];
|
||||
Array[j+1]=temp;
|
||||
swap_check=1;
|
||||
swap(numbers[j], numbers[j+1]);
|
||||
}
|
||||
}
|
||||
if(swap_check == 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
//Output
|
||||
cout<<"\nSorted Array : ";
|
||||
for(int i=0; i<n; i++)
|
||||
for(int i=0; i<numbers.size(); i++)
|
||||
{
|
||||
cout<<Array[i]<<"\t";
|
||||
if(i != numbers.size() -1)
|
||||
{
|
||||
cout << numbers[i] << ", ";
|
||||
}else
|
||||
{
|
||||
cout << numbers[i] << endl;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
55
Sorting/Counting_Sort.cpp
Normal file
55
Sorting/Counting_Sort.cpp
Normal file
@@ -0,0 +1,55 @@
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
|
||||
int Max(int Arr[], int N){
|
||||
int max = Arr[0];
|
||||
for(int i=1; i<N; i++)
|
||||
if(Arr[i] > max)
|
||||
max = Arr[i];
|
||||
return max;
|
||||
}
|
||||
|
||||
int Min(int Arr[], int N){
|
||||
int min = Arr[0];
|
||||
for(int i=1; i<N; i++)
|
||||
if(Arr[i] < min)
|
||||
min = Arr[i];
|
||||
return min;
|
||||
}
|
||||
|
||||
void Print(int Arr[], int N){
|
||||
for(int i=0; i<N; i++) cout<<Arr[i] <<", ";
|
||||
}
|
||||
|
||||
int *Counting_Sort(int Arr[], int N){
|
||||
|
||||
int max = Max(Arr, N);
|
||||
int min = Min(Arr, N);
|
||||
int *Sorted_Arr = new int[N];
|
||||
|
||||
int *Count = new int[max-min+1];
|
||||
|
||||
for(int i=0; i<N; i++) Count[Arr[i]-min]++;
|
||||
|
||||
for(int i=1; i<(max-min+1); i++) Count[i]+=Count[i-1];
|
||||
|
||||
for(int i=N-1; i>=0; i--){
|
||||
Sorted_Arr[Count[Arr[i]-min]-1] = Arr[i];
|
||||
Count[Arr[i]-min]--;
|
||||
}
|
||||
|
||||
return Sorted_Arr;
|
||||
}
|
||||
|
||||
int main(){
|
||||
|
||||
int Arr[] = {47, 65, 20, 66, 25, 53, 64, 69, 72, 22, 74, 25, 53, 15, 42, 36, 4, 69, 86, 19}, N = 20;
|
||||
int *Sorted_Arr;
|
||||
|
||||
cout<<"\n\tOrignal Array = ";Print(Arr, N);
|
||||
Sorted_Arr = Counting_Sort(Arr, N);
|
||||
cout<<"\n\t Sorted Array = ";Print(Sorted_Arr, N);
|
||||
cout<<endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -6,8 +6,10 @@ using namespace std;
|
||||
int main()
|
||||
{
|
||||
int n;
|
||||
cout<<"\nEnter the length of your array : ";
|
||||
cin>>n;
|
||||
int Array[n];
|
||||
cout<<"\nEnter any 6 Numbers for Unsorted Array : ";
|
||||
cout<<"\nEnter any "<<n<<" Numbers for Unsorted Array : ";
|
||||
|
||||
//Input
|
||||
for(int i=0; i<n; i++)
|
||||
@@ -34,5 +36,6 @@ int main()
|
||||
{
|
||||
cout<<Array[i]<<"\t";
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user