Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <time.h>
- using namespace std;
- void swap(int *xp, int *yp) {
- int temp = *xp;
- *xp = *yp;
- *yp = temp;
- }
- void bubbleSort(int arr[], int n) {
- int i, j;
- for (i = 0; i < n - 1; i++) {
- for (j = 0; j < n - i - 1; j++) {
- if (arr[j] > arr[j + 1]) {
- swap(&arr[j], &arr[j + 1]);
- }
- }
- }
- }
- int main() {
- // Initialize variable
- int c, size;
- clock_t start, end;
- // Take or generate the input
- cout << "how big do you want the array?" << endl;
- cin >> size;
- int array[size];
- srand((unsigned) time(0));
- for (int i = 0; i < size; i++) {
- array[i] = (rand() % 100) + 1;
- cout << array[i] << " ";
- }
- cout << endl;
- // Sort that input with bubble sort and findout the time
- start = clock();
- bubbleSort(array, size);
- end = clock();
- // Output the time
- double duration_in_sec = ((double) (end - start)) / CLOCKS_PER_SEC;
- cout << "Total time takes: " << duration_in_sec << "s" << endl;
- // Output the sorted element
- cout << "Sorted list in ascending order: " << endl;
- for (c = 0; c <= size - 1; c++) {
- cout << array[c] << " ";
- }
- printf("\n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement