Bubble Sort Algorithm and source code

#include<iostream>
#include<conio.h>
using namespace std;

// move the largest item to the last in one iteration
void bubbleSort(int arr[], int size){
	for(int i=0; i< size-1; i++){
// Run program to the size-i-1 because as largest 
// Item reached at best place the size unsorted array decrease
		for(int j=0; j< size-i-1; j++){
// Compare every adjacent item
			if(arr[j]>arr[j+1]){
				int temp=arr[j];
				arr[j]=arr[j+1];
				arr[j+1]=temp;
			}
		}
	}
}

int main(){ int size=6; int arr[size]={1,5,3,2,20,10}; bubbleSort(arr, size); for(int i=0; i<size; i++){ cout<< arr[i] << "\t"; } getch(); return 0; }
© 2016 - 2022, All Rights are Reserved.