0% found this document useful (0 votes)
2 views

Binary Sort in simple way using cpp

The document contains a C++ program that implements a bubble sort algorithm to sort an array of integers in ascending order. It prompts the user to enter the size of the array and its elements, displays the array before and after sorting. The sorted elements are printed to the console.

Uploaded by

mccntnd
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Binary Sort in simple way using cpp

The document contains a C++ program that implements a bubble sort algorithm to sort an array of integers in ascending order. It prompts the user to enter the size of the array and its elements, displays the array before and after sorting. The sorted elements are printed to the console.

Uploaded by

mccntnd
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

#include <iostream>

using namespace std;

void binarySort(int arr[], int size){


for(int i=0; i<size ; i++){
for(int j=0; j<size-1; j++){
if(arr[j]>arr[j+1]){
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}

cout<<"\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\nElements After Sorting:


(Ascending Order)\n";
for(int i=0; i<size; i++){
cout<<"\n\tElement # "<<i+1<<" : " << arr[i];
}
cout<<endl<<endl;
}
int main()
{
int size;

cout << "Enter the size of the array: ";


cin>>size;
//size = 3;
int arr[size];

for(int i=0; i<size; i++){


cout << "Enter the Element of the array # " << i+1 <<" : ";
cin>>arr[i];
}

cout<<"\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\nElements Before Sorting:\n";


for(int i=0; i<size; i++){
cout<<"\n\tElement # "<<i+1<<" : " << arr[i];
}
binarySort(arr,size);

return 0;
}

You might also like