#include <iostream>
#include <cstdlib>
using namespace std;
void bubblesort();
void swap(int *p1, int *p2);
int data[10];
int main () {
//Prepare data
for (int i = 0; i < 10; i++) {
data[i] = rand() % 100;
}
cout << "data before bubble sort:" << endl;
for (int i = 0; i < 10; i++)
cout << data[i] << " ";
bubblesort();
cout << "data after bubble sort:" << endl;
for (int i = 0; i < 10; i++)
cout << data[i] << " ";
return 0;
}
void bubblesort() {
int i, j, min;
int size = sizeof(data)/sizeof(int);
cout << endl << "size = " << size << endl;
for(i = 0; i < size - 1; i++) {
min = i;
for (j = i + 1; j < size; j++){
if (data[j] < data[min])
min = j;
}
if (i != min){
int tmp = data[i];
data[i] = data[min];
data[min] = tmp;
}
}
}
Thursday, September 1, 2011
Bubble Sort
標籤:
exercise
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment