Skip to content

Instantly share code, notes, and snippets.

@iaditya
Created June 4, 2018 17:24
Show Gist options
  • Save iaditya/c8422130ea35dfc07b8cb5603c56011d to your computer and use it in GitHub Desktop.
Save iaditya/c8422130ea35dfc07b8cb5603c56011d to your computer and use it in GitHub Desktop.
#include <cstdio>
#include <iostream>
#define MAX 10
using namespace std;
//function prototype
void sortBubble(int arr[], int n);
void swap(int *a, int *b);
// main function
int main() {
int n;
int arr[MAX];
printf("Enter the array length. Maximum would be 10.\n");
cin >> n;
printf("Enter numbers...\n");
for (int i=0; i < n; i++) {
cin >> arr[i];
}
sortBubble(arr, n);
printf("Sorted Array:\n");
for (int i=0; i < n; i++) {
printf("%d\t", arr[i]);
}
return 0;
}
// function declaration
void sortBubble(int arr[], int n){
for(int i=0; i< n; i++) {
for (int j=0; j < n; j++) {
if(arr[j] > arr[j+1] ) {
swap(&arr[j], &arr[j+1]);
}
}
}
}
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment