Posts

Showing posts from July, 2020

INSERTION SORTING

Insertion Sorting: code #include <iostream> using   namespace   std ; int   main (){       int   i , j , n , temp ;      int   arr [ 100 ];      cout << "Enter size of the array:" << endl ;      cin >> n ;      cout << "Enter array that to be sorted by bubble sort" << endl ;      for ( i = 0 ; i < n ; i ++ ){          cin >> arr [ i ];     }      for ( i = 1 ; i < n ; i ++ ){          temp = arr [ i ];          j = i - 1 ;          while ( j >= 0    &&   arr [ j ] > temp )...

BUBBLE SORTING TECHNIQUE

Image
              BUBBLE SORT                Bubble sorting is the simplest sorting algorithm that works by  repeatedly swapping the adjacent elements of the array. Explanation: Outer for loop I iterate for n-1 times(because if n-1 elements are sorted then automatically the nth element gets sorted.) Inner for loop checks (n-i-1) times with condition if statement: the adjacent element greater or not. if YES then swap both the number. TO optimize the code use flag variable to optimize. if the elements are swapped then flag=1, of not then flag remains unchanged, so it breaks the outer for loop. Code fo BUBBLE SORT IN CPP: #include   <iostream> using   namespace   std ; int   main () {      int  i,j,n,flag,temp;      int   arr [ 100 ];     cout << "Enter size of...