Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #define N 5
- void bubbleSort(float arr[], int n);
- void swap(float *xp, float *yp);
- void printArray(float arr[], int size);
- int main(){
- int i;
- float nums[N];
- for(i=0; i<N; ++i)
- scanf("%f", &nums[i]);
- bubbleSort(nums, N);
- return 0;
- }
- void bubbleSort(float arr[], int n) //bubble sort function
- {
- int i, j, counter=0;
- for (i=1; i < n; i++)
- for (j=0; j < n-i; j++)
- if (arr[j] > arr[j+1])
- {
- swap(&arr[j], &arr[j+1]);
- ++counter;
- printf("STEP %d: ", counter);
- printArray(arr, N);
- }
- }
- void swap(float *xp, float *yp) //function which swaps two integers
- {
- float temp = *xp;
- *xp = *yp;
- *yp = temp;
- }
- void printArray(float arr[], int size)
- {
- int i;
- for(i=0; i<size; ++i)
- printf("%.2f ", arr[i]);
- printf("\n");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement