冒泡排序,类似于水中冒泡,较大的数沉下去,较小的数慢慢冒起来,假设从小到大,即为较大的数慢慢往后排,较小的数慢慢往前排。
def bubble_sort(a_list):
for i in range(0,len(a_list)-1):
for j in range(0,len(a_list)-1-i):
if a_list[j] > a_list[j+1]:
a_list[j],a_list[j+1] = a_list[j+1],a_list[j]
return a_list
public class BubbleSort{
public static int[] bubbleSort(int[] a_list){
for(int i = 0;i<a_list.length-1;i++){
for(int j = 0;j<a_list.length-1-i;j++){
if(a_list[j]>a_list[j+1]){
int temp = a_list[j];
a_list[j] = a_list[j+1];
a_list[j+1] = temp;
}
}
}
return a_list;
}
public static void main(String[] args){
int[] arr = {
8,7,6,5,4,3,2,1};
bubbleSort(arr);
for(int x :arr){
System.out.println(x);
}
}
}
选择排序的基本思想:比较+交换
在待排序的一组数据中,选出最小(最大)的一个数与第一个位置的数交换,然后在剩下的数中,再找最小(最大)的数与第二个位置的数交换位置,
依次类推,直到第N-1个元素与第N个元素交换位置,选择排序结束。
def select_sort(a_list):
for i in range(len(a_list)-1):
min_index = i
for j in range(i+1,len(a_list)):
if a_list[j] < a_list[min_index]:
min_index = j
a_list[i],a_list[min_index] = a_list[min_index],a_list[i]
return a_list
arr2 = [76,65,24,23,3]
print(select_sort(arr2))
public class SelectSort{
public static int[] selectSort(int[] a_list){
for(int i = 0;i<a_list.length-1;i++){
int min_index = i;
for (int j=i+1;j<a_list.length;j++){
if(a_list[j]<a_list[min_index]){
min_index = j;
}
}
int temp = a_list[i];
a_list[i] = a_list[min_index];
a_list[min_index] = temp;
}
return a_list;
}
public static void main(String[] args){
int[] arr = {
8,7,6,5,4,3,2,1};
int[] res = selectSort(arr);
for(int x:res){
System.out.println(x);
}
}
}
def insert_sort(a_list):
for i in range(1,len(a_list)):
temp = a_list[i]
#从temp的位置开始,从后向前逐渐后移,为temp找到合适的位置
j = i
while j > 0 and temp < a_list[j-1]:
a_list[j] = a_list[j-1]
j-=1
a_list[j] = temp
return a_list
public class InsertSort {
public static int[] insertSort(int[] a_list){
for(int i = 1 ; i < a_list.length;i++){
int temp = a_list[i];
int j = i;
for (j=i;j> 0 && temp < a_list[j-1];j--){
a_list[j] = a_list[j-1];
}
a_list[j] = temp;
}
return a_list;
}
public static void main(String[] args){
int[] arr = {
98,65,43,32,12,2};
int[] res = insertSort(arr);
for (int x:res){
System.out.println(x);
}
}
}
希尔排序的实质就是分组插入排序,又称缩小增量法。
将整个无序序列分割成若干个子序列(由相隔某个“增量”的元素组成的)分别进行直接插入排序,然后依次缩减增量再进行排序,待整个序列中的元素基本有序时,再对全体元素进行一次直接插入排序。
因为直接插入排序在元素基本有序的情况下,效率是很高的,因此希尔排序在时间效率上有很大提高。
时间复杂度(最差 平均) (nlog(n) nlog(n))
由于多次插入排序,我们知道一次插入排序是稳定的,不会改变相同元素的相对顺序,但在不同的插入排序过程中,相同的元素可能在各自的插入排序中移动,最后其稳定性就会被打乱,所以,Shell排序是不稳定的。
def gap_insertion_sort(a_list,start,gap):
for i in range(start+gap,len(a_list),gap):
currentValue = a_list[i]
position = i
while position >= gap and a_list[position-gap] > currentValue:
a_list[position] = a_list[position-gap]
position = position - gap
a_list[position] = currentValue
def shell_sort(a_list):
sublistcount = len(a_list)//2
while sublistcount > 0:
for startposition in range(sublistcount):
gap_insertion_sort(a_list,startposition,sublistcount)
sublistcount = sublistcount//2
return a_list
arr = [54,26,93,17,77,31,44,55,20]
shell_sort(arr)
public class ShellSort {
public static int[] shellSort(int[] a_list){
int sublistcurrent = a_list.length/2;
while (sublistcurrent > 0){
for(int startposition = 0;startposition < sublistcurrent;startposition++){
gapInsertSort(a_list,startposition,sublistcurrent);
}
sublistcurrent /= 2;
}
return a_list;
}
public static void gapInsertSort(int[] a_list,int start,int gap){
for(int i = start+gap;i < a_list.length;i+=gap){
int currnetValue = a_list[i];
int position = i;
while (position >= gap && currnetValue < a_list[position-gap]){
a_list[position] = a_list[position-gap];
position -= gap;
}
a_list[position] = currnetValue;
}
}
public static void main(String[] args){
int[] arr = {
54,26,93,17,77,31,44,55,2};
int[] res = shellSort(arr);
for (int x :res){
System.out.println(x);
}
}
}
(1) 归并排序的流程
(2) 合并两个有序数组的流程
def merge_sort(a_list):
if len(a_list) > 1:
mid = len(a_list) //2
lefthalf = a_list[:mid]
righthalf = a_list[mid:]
merge_sort(lefthalf)
merge_sort(righthalf)
i,j,k = 0,0,0
while i < len(lefthalf) and j < len(righthalf):
if lefthalf[i] < righthalf[j]:
a_list[k] = lefthalf[i]
i += 1
else:
a_list[k] = righthalf[j]
j += 1
k += 1
while i < len(lefthalf):
a_list[k] = lefthalf[i]
i += 1
k += 1
while j < len(righthalf):
a_list[k] = righthalf[j]
j += 1
k += 1
return a_list
在这里插入代码片
def quick_sort(a_list):
recursive(a_list,0,len(a_list)-1)
return a_list
def recursive(a_list,low,high):
if low < high:
base_index = partition(a_list,low,high)
recursive(a_list,low,base_index)
recursive(a_list,base_index+1,high)
def partition(a_list,low,high):
base = a_list[low]
while low < high and a_list[high] > base:
high -= 1
if low < high:
a_list[low] = a_list[high]
while low < high and a_list[low] < base:
low += 1
if low < high:
a_list[high] = a_list[low]
a_list[low] = base
return low
public class QuickSort {
public static int[] quickSort(int[] a_list){
recursive(a_list,0,a_list.length-1);
return a_list;
}
public static void recursive(int[] a_list,int low,int high){
if (low < high){
int base_index = partition(a_list,low,high);
recursive(a_list,low,base_index);
recursive(a_list,base_index+1,high);
}
}
public static int partition(int[] a_list,int low,int high){
int base = a_list[low];
while (low < high && a_list[high] > base){
high --;
}
if(low < high){
a_list[low] = a_list[high];
}
while (low < high && a_list[low] < base){
low ++;
}
if (low < high){
a_list[high] = a_list[low];
}
a_list[low] = base;
return low;
}
public static void main(String[] args){
int[] arr = {
87,65,65,4237,45,432};
int[] res = quickSort(arr);
for (int x:res){
System.out.println(x);
}
}
}
def swap(a_list, i, j):
a_list[i], a_list[j] = a_list[j], a_list[i]
def heapify(a_list, i):
left = 2 * i + 1
right = 2 * i + 2
largest = i
if left < arrLen and a_list[left] > a_list[largest]:
largest = left
if right < arrLen and a_list[right] > a_list[largest]:
largest = right
if largest != i:
swap(a_list, i, largest)
heapify(a_list, largest)
def buildMaxHeap(a_list):
for i in range(len(a_list) // 2, -1, -1):
heapify(a_list, i)
def heapSort(a_list):
global arrLen
arrLen = len(a_list)
buildMaxHeap(a_list)
for i in range(len(a_list) - 1, 0, -1):
swap(a_list, 0, i)
arrLen -= 1
heapify(a_list, 0)
return a_list
在这里插入代码片