Tuesday, July 28, 2009

"Write a function in C to sort a single dimensional array of 10 integers using Bubble Sort?

Can any one help me for below question.


"Write a function in C to sort a single dimensional array of 10 integers using Bubble Sort technique."

"Write a function in C to sort a single dimensional array of 10 integers using Bubble Sort?
void bubble_sort(int *array, size_t size) {


int progress = 0;





do {


int i;


progress = 0;


for (i = 0; i %26lt; size - 2; ++i) {


if (array[i] %26gt; array[i + 1]) {


// Swap the elements


int temp = array[i];


array[i] = array[i + 1];


array[i + 1] = temp;





progress = 1;


}


}


} while (progress);


}
Reply:Given an array a of numbers, with length n





for (i=0; i%26lt;n-1; i++) {


for (j=0; j%26lt;n-1-i; j++)


if (a[j+1] %26lt; a[j]) { /* compare the two neighbours */


tmp = a[j]; /* swap a[j] and a[j+1] */


a[j] = a[j+1];


a[j+1] = tmp;


}


}


No comments:

Post a Comment