Bubble Sort
Bubble Sort
Bubble Sort
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int main(){
int arr[5]={4,3,2,1,0};
for(int i=0;i<4;i++){
for(int j=0;j<5-i-1;j++){
if(arr[j]>arr[j+1]){
swap(arr[j],arr[j+1]);
}
}
}
for(int i=0;i<5;i++){
cout<<arr[i]<<endl;
}
return 0;
}
Output –
01234
Fibonacci series
#include<iostream>
using namespace std;
int fib(int n){
if(n<=1)
return n;
return fib(n-1)+fib(n-2);
}
int main(){
int n=9;
cout<<fib(9);
for(int i =0;i<n;i++){
cout<<fib(i);
}
return 0;
}
Output -
34
011235813…
Factorial
#include<iostream>
using namespace std;
Output –
Factorial of 5 is 120
Insertion Sort
#include<iostream>
using namespace std;
int main(){
int arr[5]={3,2,1,0,3};
for(int i=1;i<5;i++){
int j=i;
while(arr[j]<arr[j-1] && j>=1){
swap(arr[j],arr[j-1]);
j--;
}
}
for(int i=0;i<5;i++){
cout<<arr[i]<<endl;
}
return 0;
}
Output –
01233
Linear Search
#include<iostream>
using namespace std;
int main(){
int arr[5] = {1,2,3,4,5};
int n;
cin>>n;
int i;
for( i=0;i<5;i++){
if(arr[i]==n){
cout<<i<<endl;
break;
}
}
if(i==5){
cout<<"NOT FOUND"<<endl;
}
return 0;
}
Output -
If n = 3 then , Output = 2
Quick Sort
#include <iostream>
using namespace std;
int partition(int arr[], int s, int e)
{
int pivot = arr[s];
int count = 0;
for (int i = s + 1; i <= e; i++)
{
if (arr[i] <= pivot)
{
count++;
}
}
int pivotIndex = s + count;
swap(arr[pivotIndex],arr[s]);
int i = s, j = e;
while (i < pivotIndex && j > pivotIndex)
{
while (arr[i] < pivot)
{
i++;
}
while (arr[j] > pivot)
{
j--;
}
if ((i < pivotIndex) && (j > pivotIndex))
{
swap(arr[i++], arr[j--]);
}
}
return pivotIndex;
}
void quick(int arr[], int s, int e)
{
if (s >= e)
return;
int p = partition(arr, s, e);
quick(arr, s, p - 1);
quick(arr, p + 1, e);
}
int main()
{
int arr[6] = {3, 2, 1, 0,-1,-2};
int n = 6;
quick(arr, 0, n - 1);
for (int i = 0; i < n; i++)
{
cout << arr[i] << " ";
}
return 0;
}
Output –
-2 -1 0 1 2 3
Matrix Multiplication
#include<iostream>
using namespace std;
int main(){
int r1,c1;
int r2,c2;
cin>>r1>>c1>>r2>>c2;
int arr[r1][c2];
int arr2[r2][c2];
int result[r1][c2];
int i,j;
for(i=0;i<r1;i++){
for(j=0;j<c1;j++){
cin>>arr[i][j];
}
}
for(i=0;i<r2;i++){
for(j=0;j<c2;j++){
cin>>arr[i][j];
}
}
for(i=0;i<r1;i++){
for(j=0;j<c2;j++){
result [i][j] = 0;
for(int k =0;k<r2;k++){
result[i][j] += arr[i][k] * arr[k][j];
}
cout<<result[i][j]<<" ";
}
cout<<endl;
}
return 0;
}
Output –
234
123
323
Tower of Hanoi
#include<iostream>
using namespace std;
void tower(int n , char from , char to ,char aux){
if(n==0){
return;
}
tower(n-1,from,aux,to);
cout<<"Move "<<n<<"From "<<from<<"To "<<to<<endl;
tower(n-1,aux,to,from);
}
int main(){
int N=3;
tower(N,'A','C','B'); return 0;
}
Output –
Move 1 From A To C
Move 2 From A To B
Move 1 From C To B
Move 3 From A To C
Move 1 From B To A
Move 2 From B To C
Move 1 From A To C
Binary Search
#include<iostream>
using namespace std;
int main()
{
int n;
cin>>n;
int arr[n];
int key;
cin>>key;
for(int i=0;i<n;i++){
cin>>arr[i];
}
int s=0;
int e=n-1;
int mid;
while(s<=e){
mid=(s+e)/2;
if(arr[mid]<key){
s=mid+1;
}
else if(arr[mid]==key){
cout<<"FOUND"<<mid<<endl;
break;
}
else{
e=mid-1;
}
}
if(s>e){
cout<<"NOT FOUND"<<endl;
}
return 0;
}
input=key=5 arr=1 2 6 5 3
output : 3
Selection Sort
#include <bits/stdc++.h>
using namespace std;
// Driver program
int main()
{
int arr[] = { 64, 25, 12, 22, 11 };
int n = sizeof(arr) / sizeof(arr[0]);
// Function Call
selectionSort(arr, n);
cout << "Sorted array: \n";
printArray(arr, n);
return 0;
}
Output –
11 12 22 25 64
Merge Sort
#include<iostream>
using namespace std;
int mainArrayIndex = s;
for(int i=0; i<len1; i++) {
first[i] = arr[mainArrayIndex++];
}
mainArrayIndex = mid+1;
for(int i=0; i<len2; i++) {
second[i] = arr[mainArrayIndex++];
}
int index1 = 0;
int index2 = 0;
mainArrayIndex = s;
delete []first;
delete []second;
}
if(s >= e) {
return;
}
int main() {
mergeSort(arr, 0, n-1);
for(int i=0;i<n;i++){
cout << arr[i] << " ";
} cout << endl;
return 0;
}
Output –
01233578
Queue
Output –
5
EMPTY
-1
EMPTY
Linked List
int s1=s;
int s2=mid+1;
int temp[e-s+1];
int k=0;
while(s1<=mid&&s2<=e){
if(arr[s1]<arr[s2]){
temp[k]=arr[s1];
s1++;
k++; }
else{ temp[k]=arr[s2];
s2++;
k++;
}}
while(s1<=mid){
temp[k]=arr[s1];
s1++;
k++;
while(s2<=e){
temp[k]=arr[s2];
s2++;
k++;
}for(int i=0;i<e-s+1;i++){
arr[s+i]=temp[i];
}}
if(s==e){
return;
int mid=(s+e)/2;
sort(arr,s,mid);
sort(arr,mid+1,e);
merge(arr,s,e,mid);}
int main()
{ int arr[6];
for(int i=0;i<6;i++){
cin>>arr[i];
sort(arr,0,5);
for(int i=0;i<6;i++){
cout<<arr[i]<<" ";
}cout<<endl;
return 0;
Input : 65 8 2 4 5 56
Output: 2 4 5 8 56 65