Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // C program for implementation of Bubble sort
- #include <stdio.h>
- #include <iostream>
- #include <cstdlib>
- #include <ctime>
- using namespace std;
- void swap(int *xp, int *yp)
- {
- int temp = *xp;
- *xp = *yp;
- *yp = temp;
- }
- // A function to implement bubble sort
- void bubbleSort(int arr[], int n)
- {
- int i, j;
- for (i = 0; i < n-1; i++)
- // Last i elements are already in place
- for (j = 0; j < n-i-1; j++)
- if (arr[j] > arr[j+1])
- swap(&arr[j], &arr[j+1]);
- }
- /* Function to print an array */
- void printArray(int arr[], int size)
- {
- int i;
- for (i=0; i < size; i++)
- printf("%d ", arr[i]);
- }
- // Driver program to test above functions
- int main()
- {
- int array_size;
- cin>>array_size;
- int arr[array_size];
- // = sizeof(arr)/sizeof(arr[0]);
- for(int i = 0; i < array_size; i++){
- arr[i] = (rand() % 20);
- }
- const clock_t begin_time = clock();
- bubbleSort(arr, array_size);
- std::cout << "Execution time "<< endl;
- std::cout << float( clock () - begin_time ) / 1000;
- printf("Sorted array: \n");
- printArray(arr, array_size);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement