funcbubbleSort(nums []int){ // 外循环:未排序区间为 [0, i] fori :=len(nums) -1; i >0; i-- { // 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端 forj :=0; j < i; j++ { ifnums[j] > nums[j+1] { // 交换 nums[j] 与 nums[j + 1] nums[j], nu...
void bubbleSort(vector<int>& arr) { // Your code here int n = arr.size(); for(int i = n-1 ; i>0 ; i--){//outer loop-no of iterations ie n-1 for(int j = 0 ; j<=n-2 ; j++){//inner loop - swapping the emements ie if(arr[j+1]<arr[j]){ int temp = arr...
for (i in 0..<n) count++ return count @@ -55,7 +54,9 @@ fun bubbleSort(nums: IntArray): Int { for (j in 0.. nums[j + 1]) { // 交換 nums[j] 與 nums[j + 1] nums[j] = nums[j + 1].also { nums[j + 1] = nums[j] } val temp = nums[j] nums[j] = nums...
defbubble_sort(nums:list[int]): """冒泡排序"""# 升序:把最大值“冒泡”到右边 n=len(nums) foriinrange(n-1,0,-1):# 外循环:右 [n-1, 1] 左 flag=False# 若内层循环未发生交换说明已经排好 forjinrange(i):# 内循环:左 [0, i-1] 右 ...
February 7:unnamed update, likelyassociated with Panda& some of the AI aspects of their relevancy scoring for the quality of user experience March 7th - 9th:update Fred April 25th:Project Owladded direct user feedback to featured snippets & search suggestions, while the barrier to entry in orde...
The Bubble Sort algorithm can be improved a little bit more. Imagine that the array is almost sorted already, with the lowest numbers at the start, like this for example: my_array=[7,3,9,12,11] In this case, the array will be sorted after the first run, but the Bubble Sort algorit...
The time complexity for the Selection Sort algorithm can be displayed in a graph like this:As you can see, the run time is the same as for Bubble Sort: The run time increases really fast when the size of the array is increased.
for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { BubbleSort(tt.nums) t.Log(tt.nums) }) } } 3 changes: 3 additions & 0 deletions 3 go.mod Original file line numberDiff line numberDiff line change @@ -0,0 +1,3 @@ module incq.pro/golang-algo go 1.17 ...
for (i in 0..<n) count++ return count @@ -55,7 +54,9 @@ fun bubbleSort(nums: IntArray): Int { for (j in 0.. nums[j + 1]) { // 交換 nums[j] 與 nums[j + 1] nums[j] = nums[j + 1].also { nums[j + 1] = nums[j] } val temp = nums[j] nums[j] = num...
for (i in 0..<n) count++ return count @@ -55,7 +54,9 @@ fun bubbleSort(nums: IntArray): Int { for (j in 0.. nums[j + 1]) { // 交換 nums[j] 與 nums[j + 1] nums[j] = nums[j + 1].also { nums[j + 1] = nums[j] } val temp = nums[j] nums[j] = nums...