首页 > 试题广场 >

(数据结构与算法)列举至少2种排序算法(如快排),并写出实现

[问答题]

(数据结构与算法)列举至少2种排序算法(如快排),并写出实现代码

def quick_sort(num, left, right):
key =num[0]
low =left
high =right
while left<right:
while left<right and key<=num[right]:
right-=1
num[left] =num[right]
while left<right and key>num[left]:
left -=1
num[right] =num[left]
num[right] =key
quick_sort(sum, low, left-1)
quick_sort(sum, left+1, high)
return num
def merge_sort(num):
if len(num)<=1: return num
center =len(num)//2
left =merge_sort(num[:center])
right =merge_sort(num[center:])
return merge(left, right)
def merge(num1, num2):
i ,j =0, 0
result =[]
while i<len(num1) and j<len(num2):
if num1[i]<num2[j]:
result.append(num1[i])
i +=1
else:
result.append(num2[j])
j +=1
result +=num1[i:]
result +=num2[j:]
return result
if__name__ =="__main__":
num1 =input()
num2 =input()
num1 =quick_sort(num1, 0,len(num1)-1)
num2 =merge_sort(num2)
print(num1)
print(num2)

编辑于 2019-07-22 11:13:52 回复(2)
翻《数据挖掘》课本
发表于 2021-09-24 13:45:21 回复(0)
1.冒泡排序
1
2
3
4
5
6
7
8
9
10
11
def bubbleSort(numlist):
    N = 0
    key = True
    while key and N < len(numlist):
        key = False
        for i in range(len(numlist)-N):
            if numlist[i]>numlist[i+1]:
                numlist[i],numlits[i+1] = numlist[i+1],numlist[i]
                N = N + 1
                key  = True
    returnnumlist
2.插入排序
1
2
3
4
5
6
7
def insertionSort(numlist):
    for i in rang(1,len(numlist)):
        N = 0
        while i> N and numlist[i-N]<numlist[i-N-1]:
            numlist[i-N], numlist[i-N-1] = numlist[i-N-1],numlist[i-N]
            N = N + 1
    return numlist
发表于 2020-09-18 15:14:49 回复(0)
public void quickSort(int[] arr){
int m  = partition(arr,0,arr.length-1);
partition(arr,0,m-1);
partition(arr,m+1,arr.length-1);
}
private int partition(int[] arr,int start,int end){
int flag = arr[start];
int i = start,j = end;
while (i < j){
while(i < j &&arr[j] >= flag) j--;
arr[i]= arr[j];
while(i < j && arr[j] <= flag) i++;
arr[j] = arr[i];
}
arr[i] = flag;
return i;
}
public void merge(int[] arr,int start,int end,int mid){
int i = start,m = mid,j = mid+1,n = end;
int[] temp = new int[end-start+1];
while (i <= m && j <= n){
if (arr[i] <= arr[j]) temp[k++] = arr[i++];
else temp[k++] = arr[j++];
}
while (i <= m) temp[k++] = arr[i++];
while (j <= n) temp[k++] = arr[j++];
for(int i = 0; i < k; i++){
arr[start+i] = temp[i];
}
}
public void mergeSort(int[] arr,int start,int end){
int mid = (start+end)/2;
if (start < end){
mergeSort(arr,start,mid);
mergeSort(arr,mid+1,end);
merge(arr,start,end,mid);
}
}
public void sort(int arr){
mergeSort(arr,0,arr.length-1);
}

编辑于 2018-03-24 10:09:01 回复(0)
归并排序:
-- NORMAL --
void merge_sort(int a[], int x, int y, int t[]){
  if(y - x > 1)
  {
    int m = x + (y - x) / 2;
    int p = x, q = m;
    int i = x;
    merge_sort(a, x, m, t);
    merge_sort(a, m, y, t);
    while(p < m || q < y)
    {
      if(q >= y || (p < m && a[p] <= a[q]))
      {
        t[i++] = a[p++];
      }else
      {
        t[i++] = a[q++];
      }
    }
    for (int i = x; i < y; ++i)
    {
      a[i] = t[i];
    }
  }
}
快速排序:
int partation(int a[], int x, int y){
  int p = x - 1, q = y;
  int val = a[y];
  int i = x;
  while(i < q) {
    if(a[i] < val)
    {
      swap(a[++p], a[i++]);
    }else if(a[i] > val)
    {
      swap(a[--q], a[i]);
    }
    else {
      i++;
    }
  }
  swap(a[q], a[y]);
  return q;
}

void quick_sort(int a[], int x, int y){
    if(x < y)
    {
      int p = partation(a, x, y);
      quick_sort(a, x, p - 1);
      quick_sort(a, p + 1, y);
    }
}

发表于 2018-03-03 20:32:29 回复(0)
# 实现版本python 3.5
# L = [3,1,7,12,3,5]为例 # 1.冒泡排序(Bubble sort)简易版
L = [3, 1, 7, 12, 3, 5] fori inrange(len(L) -1):
    forj inrange(len(L) -i -1):
        ifL[j] > L[j+1]:
            L[j], L[j +1] =L[j +1], L[j]
print('new list:', L)
#2.快排(Quick Sort)
L =[3, 1, 7, 12, 3, 5]
defQuicksort(l =L, fi, li):
    iffi < li:
        di =Partition(l, fi, li)
        Quicksort(l, fi, di)
        Quicksort(l,di +1, li)
    else:
        return
 
defPartition(l, fi, li):
    i =fi -1
    forj inrange(fi, li):
        ifl[j] <=l[li]:
            i +=1
            l[i], l[j] =l[j], l[i]
    l[i+1], l[li] =l[li], l[i+1]
    returni
Quicksort(L, 0, len(L) -1)
print('new list:', L)

发表于 2018-02-28 20:52:25 回复(0)
quicksort & mergersort
发表于 2018-02-15 05:17:54 回复(0)