Monday, November 17, 2025

INSERTION SORT IN CPP

 #include <iostream>

 using namespace std;
 void PrintArray(int A[],int n){
    for(int i=0;i<n;i++){
        cout<<A[i]<<endl;
    }
 }
 void InsertionSort(int A[],int n){
    for(int i=1;i<n;i++){
        int key=A[i];
       int j=i-1;
        while(j>=0 && key<A[j]){
            A[j+1]=A[j];
            j--;
        }
        A[j+1]=key;
    }
 }
 int main() {
 //Write your C++ code here
 cout << "Hello, World!" << endl;
 int A[]={1,34,56,67,433,221};
 int n= sizeof(A)/sizeof(int);
 InsertionSort(A,n);
 PrintArray(A,n);
 return 0;
 }

Sunday, November 16, 2025

selection sort in cpp

#include <iostream>
 using namespace std;
 void PrintArray(int A[],int n){
    for(int i=0;i<n;i++){
        cout<<A[i]<<endl;
    }
 }
 void SelectionSort(int A[], int n) {
    for(int i = 0; i < n - 1; i++) {
        int Min = i;  // assume current i is the minimum

        for(int j = i + 1; j < n; j++) {
            if(A[j] < A[Min]) {
                Min = j;  // found new minimum
            }
        }

        // swap A[i] and A[Min]
        int temp = A[i];
        A[i] = A[Min];
        A[Min] = temp;
    }
}

 int main() {
 int A[]={12,35,34,75,56,24};
 int n=sizeof(A)/sizeof(int);
 SelectionSort(A,n);
PrintArray(A,n);
 return 0;
 }

Saturday, November 15, 2025

BUBBLE SORT IN CPP

 #include <iostream>

 using namespace std;
 void SortArray(int a[],int n){
int temp;
for(int i=0;i<n;i++){
    for(int j=0;j<n-i-1;j++){
        if(a[j]>a[j+1]){
            temp=a[j];
            a[j]=a[j+1];
            a[j+1]=temp;
        }
    }
}
 }
 int main() {
 //Write your C++ code here
 int A[]={1,34,78,54,75,12};
 int n=6;
 SortArray(A,n);
 for(int i=0;i<6;i++){
    cout<<A[i]<<endl;
 }
 return 0;
 }

MergeSort

  #include < iostream >   using namespace std ;   void merge ( vector < int >& n , int low , int mid , int high ){   ...