Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Bubble Sort

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 20

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;

int factorial( int n)


{
if (n == 0 || n == 1)
return 1;
return n * factorial(n - 1);
}
int main(){
int num = 5;
cout << "Factorial of "
<< num << " is " << factorial(num) << endl;
return 0;
}

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;

// Function for Selection sort


void selectionSort(int arr[], int n)
{
int i, j, min_idx;

// One by one move boundary of


// unsorted subarray
for (i = 0; i < n - 1; i++) {

// Find the minimum element in


// unsorted array
min_idx = i;
for (j = i + 1; j < n; j++) {
if (arr[j] < arr[min_idx])
min_idx = j;
}

// Swap the found minimum element


// with the first element
if (min_idx != i)
swap(arr[min_idx], arr[i]);
}
}

// Function to print an array


void printArray(int arr[], int size)
{
int i;
for (i = 0; i < size; i++) {
cout << arr[i] << " ";
cout << endl;
}
}

// 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;

void merge(int *arr, int s, int e) {

int mid = (s+e)/2;

int len1 = mid - s + 1;


int len2 = e - mid;

int *first = new int[len1];


int *second = new int[len2];

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;

while(index1 < len1 && index2 < len2) {


if(first[index1] < second[index2]) {
arr[mainArrayIndex++] = first[index1++];
}
else{
arr[mainArrayIndex++] = second[index2++];
}
}

while(index1 < len1) {


arr[mainArrayIndex++] = first[index1++];
}

while(index2 < len2 ) {


arr[mainArrayIndex++] = second[index2++];
}

delete []first;
delete []second;
}

void mergeSort(int *arr, int s, int e) {

if(s >= e) {
return;
}

int mid = (s+e)/2;


mergeSort(arr, s, mid);
mergeSort(arr, mid+1, e);
merge(arr, s, e);

int main() {

int arr[15] = {3,7,0,1,5,8,3,2};


int n = 15;

mergeSort(arr, 0, n-1);

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

return 0;
}

Output –
01233578
Queue

Traversing , Insertion and Deletion -


#include<iostream>
using namespace std;
int n= 20;
class queue{
int *arr;
int rear ;
int front;
public:
queue(){
arr = new int[n];
front =-1;
rear = -1;
}
void enqueue(int x){
if(rear == n-1){
cout<<"Overflow"<<endl;
return ;
}
rear++;
arr[rear] = x;
if(front ==-1){
front++;
}
}
void dequeue (){
if(front == -1 || front>rear){
cout<<"EMPTY"<<endl;
return ;
}
front++;
}
int traverse(){
if(front == -1 || front>rear){
cout<<"EMPTY"<<endl;
return -1;
}
return arr[front];
}
bool empty (){
if(front ==-1 || front>rear){
return true ;
}
return false ;
}
};
int main(){
queue q;
q.enqueue(5);
q.enqueue(9);
cout<<q.traverse()<<endl;
q.dequeue();
cout<<q.traverse()<<endl;
q.dequeue();
cout<<q.traverse()<<endl;
q.dequeue();
return 0;
}

Output –
5

EMPTY

-1

EMPTY
Linked List

Insertion and Deletion -


#include<iostream>

using namespace std;

void merge(int *arr,int s,int e,int mid){

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];

}}

void sort(int *arr,int s,int e){

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

You might also like