joun17 be cool | I'm attempting to figure out what's wrong with my code; I'm new to C but have a lot of experience with Java. I created my own "lazy" bubble sort implementation using 8 integers. However, it results in an indefinite loop. I read several articles about Bubble sort in C, https://www.scaler.com/topics/c-bubble-sort/.
Attempting to sort 8 integers from greatest to smallest and using the counter to detect when all values are sorted.
Code :
- #include <stdio.h>
- int main()
- {
- int array[8];
- int counter =0;
- int storage=0;
- int i;
- printf("Please enter 8 numbers:" );
- scanf("%d%d%d%d%d%d%d%d",&array[0],&array[1],&array[2],&array[3],&array[4],&array[5],&array[6],&array[7]);
- while (counter!=7)
- {
- counter =0;
- for (i=0; i<=6;i++)
- {
- if (array[i]<=array[i++])
- {
- storage = array[i];
- array[i]= array[i++];
- array[i++]= storage;
- }
- else
- {
- counter++;
- }
- }
- }
- printf("%d%d%d%d%d%d%d%d",array[0],array[1],array[2],array[3],array[4],array[5],array[6],array[7]);
|
|