adsense

Hii, welcome to my site. My name is Om prakash kartik. This blog helps you to learn programming languages concepts.

Program in C and C++ to sort array using advance bubble sort technique.

 Program in C and C++ to sort array using advance bubble sort technique


C Program
#include<stdio.h>
 void bubble_sort_advance(int *arr, int size){
      int i, j, temp = 0, flag;
      for(i = 0; i < size - 1; i++){
        flag = 0;
        for(j = 0; j < size - 1 - i; j++){
            if(arr[j] > arr[j + 1]){
                temp = arr[j + 1];
                arr[j + 1] = arr[j];
                arr[j] = temp;
                flag = 1;
            } // if statement
        } // inner for loop
        if(flag == 0)
            break;
     } // outer for loop
 } // end bubbleSort
 int main(){
    int arr[] = {33, 22, 55, 44, 66, 11};
    int size = 6, i;
    printf("Advance bubble sort technique\n");
    printf("\nBefore sorting..");
    for(i = 0; i < size; i++){
        printf("%4d", arr[i]);
    }
    //
    bubble_sort_advance(arr, size);

    printf("\nAfter sorting..");
    for(i = 0; i < size; i++){
        printf("%4d", arr[i]);
    }
   return 0;
 }
C++ Program
#include<iostream>
using namespace std;
 void bubble_sort_advance(int *arr, int size){
      int i, j, temp = 0, flag;
      for(i = 0; i < size - 1; i++){
        flag = 0;
        for(j = 0; j < size - 1 - i; j++){
            if(arr[j] > arr[j + 1]){
                temp = arr[j + 1];
                arr[j + 1] = arr[j];
                arr[j] = temp;
                flag = 1;
            } // if statement
        } // inner for loop
        if(flag == 0)
            break;
     } // outer for loop
 } // end bubbleSort
 int main(){
    int arr[] = {15, 6, 25, 44, 66, 11, 65, 3, 45};
    int size = 9, i;

    cout<<"Advance bubble sort technique\n";
    cout<<endl<<"Before sorting..";
    for(i = 0; i < size; i++){
        cout<<"  "<<arr[i];
    }
    //
    bubble_sort_advance(arr, size);

    cout<<endl<<"After sorting..";
    for(i = 0; i < size; i++){
        cout<<"  "<<arr[i];
    }
   return 0;
 }
Output :-











Related programs 

Post a Comment

0 Comments