Also, if we observe the code, bubble sort requires two loops. Hence, the complexity isn*n = n2 1. Time Complexities Worst Case Complexity:O(n2) If we want to sort in ascending order and the array is in descending order then the worst case occurs. ...
intarray[] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1}; bubble_sort(array, 10); std::for_each(&array[0], &array[10], print<int>); std::cout << std::endl; intdata[] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1}; std::sort(data, data+10); std::for_each(data, data...
冒泡排序(Bubble Sort)也是一种简单直观的排序算法。它重复地走访过要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来。走访数列的工作是重复地进行直到没有再需要交换,也就是说该数列已经排序完成。这个算法的名字由来是因为越小的元素会经由交换慢慢"浮"到数列的顶端。
cout<<endl<<"later:"<<endl; BubbleSort(nData,nLength); Output(nData,nLength); } 嗯,还有优化的空间。 如果在一次扫描的过程中,没有交换发生,则说明已经排好序了,回此,可以提前结束,而不必进行接下来多躺无用的比较。 同样是写冒泡,质量就在这里。
(2) your codeSwap(arr[j-1],arr[j]);}}}voidBubbleSort(int*arr,intn){for(inti=0;i<n;i++){// i-th passBubble(arr,n-i);//put min element in the frontprint_array(arr,n);}}intmain(){intn=8;int*arr=newint[n]{42,20,17,13,28,14,23,15};//申请8个int变量,并初始化...
Gaminic(1621) You're code (and logic) is a bit messy, but at first sight it should work. Just add the line "swap = 0" behind line 50. Now, if a single swap is made, swap is set to 1. This never changes back to 0, hence the infinite loop. ...
开发者ID:jfujihub,项目名称:league,代码行数:56,代码来源:filein.c 示例2: bubblesort_two_strings ▲点赞 6▼ /** * Test thatbubblesort() correctly sorts an array of two * strings. */staticvoidbubblesort_two_strings(void){char*a[2];/* The array is already sorted - no designation */...
include <stdio.h>void swap(int *a, int *b);void bubbleSort(int array[], int length){int i,j;for(i = 0; i < length - 1; i++) {for(j = 0; j < length - i - 1; ++j) {if(array[j] > array[j + 1])swap(&array[j],&array[j + 1]);}}} void swap(...
Source File: bubble_sort.c From Algorithms with MIT License 6 votes void bubble_sort(int arr[], int n) { int i, j; int swapped; for (i = 0; i < n - 1; ++i) { swapped = 0; for (j = 0; j < n - i - 1; ++j) { if (arr[j] > arr[j+1]) { swap(&arr[j]...
http://www.waitingfy.com/?p=403 Related Posts LeetCode Sort Colors LeetCode Sort C… Inserting Sort (插入排序) Since an array … Python 冒泡排序 Python 冒泡排序 Quick Sort (快速排序 C++) Below is the co… 找到单独的数字 Single Number Given an array … 403...