Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions c++/Bubble_Sort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <iostream>
using namespace std;

int main() {

int i,j,a[1000],n;
cin>>n;

for (i=0;i<=n-1;i++){
cin>>a[i];
}
for (i=0;i<n-1;i++){
for(j=0;j<n-1-i;j++){
if(a[j]>a[j+1]){
swap(a[j],a[j+1]);
}
}
}

for (i=0;i<=n-1;i++){
cout<<a[i]<<" ";
}


return 0;
}
42 changes: 42 additions & 0 deletions c++/InBuilt_Sort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include<bits/stdc++.h>
using namespace std;

bool compare1(int a, int b){

return a < b;
}

bool compare2(int a, int b){

return a > b;
}
int main(){
int i,j,n;
cin>>n;

int a[n];

for(i=0;i<=n-1;i++){
cin>>a[i];
}

sort( a , a+n );

for(i=0;i<=n-1;i++){
cout<<a[i]<<" ";
}
cout<<endl;

sort(a, a+n, compare1);

for(i=0;i<=n-1;i++){
cout<<a[i]<<" ";
}
cout<<endl;

sort(a, a+n, compare2);

for(i=0;i<=n-1;i++){
cout<<a[i]<<" ";
}
}
33 changes: 33 additions & 0 deletions c++/Insertion_Sort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//THIS IS not from cb ....from jennys lecture

#include <iostream>
using namespace std;

int main() {
int i, j,n,temp;
cin>>n;
int a[n];

for(i=0;i<=n-1;i++){
cin>>a[i];
}

for(i=1;i<=n-1;i++){
temp = a[i];
j = i-1;
while(j>=0 && a[j]>temp){

a[j+1] = a[j];
j-- ;
}

a[j+1] = temp;
}

for(i=0;i<=n-1;i++){
cout<<a[i]<<" ";
}


return 0;
}
30 changes: 30 additions & 0 deletions c++/Linear_search.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include<bits/stdc++.h>
using namespace std;

int main(){

int n,key;
cin>>n;

int a[n],i;

for( i=0;i<n;i++){
cin>>a[i];
}

cout<<"Enter the digit u want to find "<<endl;
cin>>key;

for( i =0;i<n;i++){
if(a[i]==key){
cout<<i<<endl;
break;
}
}
if( i == n ){
cout<<"Not found"<<endl;
}

return 0;

}
57 changes: 57 additions & 0 deletions c++/MedianOfTwoSortedArray.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;

int Solution(int arr[], int n)
{

// If length of array is even
if (n % 2 == 0)
{
int z = n / 2;
int e = arr[z];
int q = arr[z - 1];
int ans = (e + q) / 2;
return ans;
}

// If length if array is odd
else
{
int z = round(n / 2);
return arr[z];
}
}

// Driver Code
int main() {

// TODO Auto-generated method stub
int arr1[] = {2, 3, 5, 8};
int arr2[] = {10, 12, 14, 16, 18, 20};

int i = sizeof(arr1) / sizeof(arr1[0]);
int j = sizeof(arr2) / sizeof(arr2[0]);

int arr3[i+j];
int l = i+j;
// Merge two array into one array
for(int k=0;k<i;k++)
{
arr3[k]=arr1[k];
}

int a=0;
for(int k=i;k<l;k++)
{
arr3[k]=arr2[a++];
}

// Sort the merged array
sort(arr3,arr3+l);

// calling the method
cout<<"Median = " << Solution(arr3, l);
}

// This code is contributed by SoumikMondal
40 changes: 40 additions & 0 deletions c++/Selection_Sort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include<bits/stdc++.h>
using namespace std;

void selectionSort(int a[],int n){

for(int i=0;i<n-1;i++){
int min_index = i;
for(int j=i;j<=n-1;j++){

if(a[min_index]>a[j]){
min_index = j;
}
}

swap(a[i],a[min_index]);
}

}

int main(){

int n,key;
cin>>n;

int a[n];

for(int i=0;i<n;i++){
cin>>a[i];
}

selectionSort(a,n);

for(int i=0;i<n;i++){
cout<<a[i]<<" ";
}


return 0;

}
27 changes: 27 additions & 0 deletions c++/SubArraySUM_Kadanes_algo.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include<bits/stdc++.h>
using namespace std;
int main(){
int i,curr_sum = 0 , max_sum =0,n;
cin>>n;

int a[n];

for(i=0;i<n;i++){
cin>>a[i];
}

for(i=0;i<n;i++){
curr_sum = curr_sum + a[i];

if(curr_sum<0){
curr_sum = 0;
}

max_sum = max(max_sum, curr_sum); //this max funt'n gives the maximum of the two values among these 2 variables
}

cout<<"maximum sum of a subarray is: "<<max_sum<<endl;

return 0;

}
29 changes: 29 additions & 0 deletions c++/Two_pointerApproach_PairSUM.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include<bits/stdc++.h>
using namespace std;
int main(){
int i=0 ,j, a[] = {1,3,5,7,9,11,13,14 } , sum=14; //this method only works for sorted arrays to give pairs having a particular sum
j = sizeof(a)/sizeof(int) - 1; //it gives us n-1

while(i<j){

int curr_Sum = a[i] + a[j];

if(curr_Sum > sum ){
j--; //since in sorted array if i moves it will only increase its value so j--
}

else if (curr_Sum < sum){
i++;
}

else if(curr_Sum == sum){

cout<<a[i]<<" ,"<<a[j]<<endl;
i++;
j--;
}

}

return 0;
}
45 changes: 45 additions & 0 deletions c++/binary_search.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//Rohit

#include<bits/stdc++.h>
using namespace std;

int binarySearch(int a[],int n,int key){
int s=0,mid,e= n-1;

while(s<=e){

mid=(s+e)/2;

if(a[mid]==key){
return mid; //will return the value of the index and loop and funt'n will stop here
}
else if (a[mid]>key){
e = mid - 1;
}
else if(a[mid]<key){
s = mid+1;
}

}

return -1; // -1 means not found
}
int main(){

int n,key;
cin>>n;

int a[n];

for(int i=0;i<n;i++){
cin>>a[i];
}

cout<<"Enter the digit u want to find "<<endl;
cin>>key;

cout<<binarySearch(a,n,key);

return 0;

}