对于一个int数组,请编写一个冒泡排序算法,对数组元素排序。
给定一个int数组A及数组的大小n,请返回排序后的数组。
测试样例:
[1,2,3,5,2,3],6
[1,2,2,3,3,5]
我的做法:
# -*- coding:utf-8 -*-
class BubbleSort:
def bubbleSort(self, A, n):
# write code here
for j in range (1,n):
for i in range(0,n-1 ):
if (A[i]>A[i+1]):
A[i],A[i+1] = A[i+1],A[i]
return A
参考:
# -*- coding:utf-8 -*-
class BubbleSort:
def bubbleSort(self, A, n):
# write code here
A.sort()
return A
或者
# -*- coding:utf-8 -*-
class BubbleSort:
def bubbleSort(self, A, n):
# write code here
return sorted(A)