Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Bubble-Sort.cpp #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 62 additions & 34 deletions Bubble-Sort.cpp
Original file line number Diff line number Diff line change
@@ -1,39 +1,67 @@
#include <cstdio>
#include <cstdlib>

#include <iostream>
using namespace std;

int data[1000];

void bubble_sort(int *d, int n)
{
for (int k = 1; k < n; k++)
{
for (int i = 1; i < n; i++)
{
if (d[i] < d[i - 1])
{
int temp = d[i];
d[i] = d[i - 1];
d[i - 1] = temp;
}
}
}
void bubbleSort(int array[], int size, int order){

if(order == 1){
for(int i=0; i < size-1; i++){
int flag = 0;

for(int j=0; j<size-1-i; j++){
if(array[j] > array[j+1]){
swap(array[j+1], array[j]);
flag = 1;
}
}

if(flag == 0){
break;
}
}
}

else if(order == 2){
for(int i=0; i < size-1; i++){
int flag = 0;

for(int j=0; j<size-1-i; j++){
if(array[j] < array[j+1]){
swap(array[j+1], array[j]);
flag = 1;
}
}

if(flag == 0){
break;
}
}
}

}

int main()
{
for (int i = 0; i < 1000; i++)
{
data[i] = rand();
}

bubble_sort(data, 1000);

for (int i = 0; i < 1000; i++)
{
printf("%d\n", data[i]);
}

return 0;
int main() {
int size;
int order;

cout << "Enter the size of the array:" << endl;
cin >> size;

int array[size];

cout << "Enter the elements of the array:" << endl;
for(int i = 0; i < size; i++){
cin >> array[i];
}

cout << "What type of ordering do you want: \n 1 - Ascending \n 2 - Descending" << endl;
cin >> order;

bubbleSort(array, size, order);

cout << "The sorted array is:" <<endl;
for (int i = 0; i < size; i++) {
cout << array[i] << " ";
}

return 0;
}