Switch to full style
For C/C++ coders discussions and solutions
Post a reply

C++ Bubble Sort

Sun Sep 07, 2008 6:11 pm

Well I'm kinda new to programming and since i didn't see this sort on this site I thought that I'd post mine. Just made this last night just to see if I could. If you see any errors please point them out.
cpp code
#include<iostream>

int* BubbleSort(int* a);

using namespace std;

void main()
{

int sort[10], i;
int* psort = 0;
psort = sort;

printf("Enter 10 numbers: \n");
for(i=0; i<10; i++) {
printf("%d: ", (i+1));
cin >> sort[i];
}

printf("\nBefore sorted: ");
for(i=0; i < 10; i++) {
printf("%d ", sort[i]);
}

printf("\n\n After Sorted: ");
psort = BubbleSort(psort);

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

int* BubbleSort(int* a) {

int c = 0;
int d = 0;

do {
d = 0;
for(int b = 0; b < 9; b++) {
if(a[b] > a[b+1]) {
c = a[b];
a[b] = a[b+1];
a[b+1] = c;
d++;
}
}
}while(d);

return a;
}

This algorithm is one of the simplest and easiest to understand but it is very inefficient so I wouldn't use it for anything serious.
This sorts out 10 integers in an array and puts them from lowest to highest order.



Re: C++ Bubble Sort

Sun Sep 07, 2008 7:57 pm

Thanks "Shimano" for your post :gOOd: :gOOd: :gOOd: :gOOd: .

About my self i made the bubble sort before by my self but i don;t know where the code gone :swoon: . thanks and keep sharing your work , i am sure it will be helpful to someone .

Post a reply
  Related Posts  to : C++ Bubble Sort
 bidirectional bubble sort algorithm implementation java     -  
 Bubble Sort Algorithm Java Implementation Code-Sorting Array     -  
 Library Sort     -  
 String Sort     -  
 selection sort     -  
 Sort a list     -  
 Insertion Sort (C++)     -  
 Array sort     -  
 C++ Recursion Sort     -  
 C++ Selection Sort     -  

Topic Tags

C++ Sorting