Program in C and C++ to sort array using bubble sort technique
C Program
#include<stdio.h> void bubble_sort(int *arr, int size){ int i, j, temp = 0; for(i = 0; i < size - 1; i++){ 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; } } } } int main(){ int arr[] = {33, 22, 55, 44, 66, 11}; int size = 6, i; printf("\nBefore sorting.."); for(i = 0; i < size; i++){ printf("%4d", arr[i]); } // bubble_sort(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(int *arr, int size){ int i, j, temp = 0; for(i = 0; i < size - 1; i++){ 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; } } } } int main(){ int arr[] = {15, 6, 25, 44, 66, 11, 65, 3, 45}; int size = 9, i; cout<<endl<<"Before sorting.."; for(i = 0; i < size; i++){ cout<<" "<<arr[i]; } // bubble_sort(arr, size); cout<<endl<<"After sorting.."; for(i = 0; i < size; i++){ cout<<" "<<arr[i]; } return 0; }
Output :-
0 Comments