python版(随机化&置换排序版replacement order)
'''
Description:
Version: 2.0
Author: xuchaoxin
Date: 2021-03-26 21:00:45
LastEditors: xuchaoxin
LastEditTime: 2021-03-27 19:49:00
'''
import generate_randomInt
import random
""" partition:we need the state that meet the condition:elements in the left part <= pivot_element;and elements in the right part >=pivot_element;the pivot_element is in the proper location of the sequence finally
it's no more than to describe the demand of the function to achieve a certain algorithm!!
unless you are very familiar to what you write (otherwise ,don't overestimate the degree your understand of the algorithm you are writing;it's time for you to formulate a series of regularity/norm
"""
def partition (dataList, pivot_index ):
"""这个函数只是为了让quick_sort()内部代码结构更紧凑而提取出来的(不是必须的)
Args:
dataList (list): [description]
pivot_index (Number): [description]
rightList (List): [description]
leftList (List): [description]
"""
size_of_dataList = len (dataList)
""" set two index variable to partition the sequence """
i = 0
j = size_of_dataList-1
""" save the pivot_element to be compared: """
pivot_element = dataList[pivot_index]
""" swap the pivot generated randomly,transform the familiar problem to solve """
dataList[pivot_index], dataList[0 ] = dataList[0 ], dataList[pivot_index]
while (i < j):
while (i < j and dataList[j] >= pivot_element):
j -= 1
if i < j:
dataList[i] = dataList[j]
i += 1
while (i < j and dataList[i] < pivot_element):
i += 1
if i < j:
dataList[j] = dataList[i]
j -= 1
""" the swap operation may not appropriate in the partition process """
""" the pivot insert to the proper place finally """
dataList[i] = pivot_element
""" update the pivot_index after the pass of the partition """
pivot_index = j
""" return to make divide to conquer subproblem """
return pivot_index
def quick_sort (dataList ):
"""利用递归来实现快速排序(随机轴心+置换版)
<<<
Args:
dataList (list): 待排序的数列
Returns:
list: 排好序的数列!!!>>>
"""
if len (dataList) >= 2 :
""" 选取基准值,也可以选取第一个或最后一个元素(注意这里用地板除法)
这个基准值将要特地取出来,会用来连接左侧序列和右侧序列(也作为关节元素)
"""
pivot_index = random.randint(0 , len (dataList)-1 )
pivot_element = dataList[pivot_index]
""" 逐个判断数列中的各个元素和基准值的大小关系,并放到对应侧的子序列中
可以考虑封装到一个函数中去"""
pivot_index = partition(dataList, pivot_index)
leftList, rightList = dataList[0 :pivot_index], dataList[pivot_index+1 :]
pivot_element = dataList[pivot_index]
""" 递归调用:对两侧子序列分别调用quick_sort()处理 """
return quick_sort(leftList) + [pivot_element] + quick_sort(rightList)
else :
return dataList
array = generate_randomInt.generate()
print ("the input sequence:\n" ,array)
print ("the sorted sequence:" )
print (quick_sort(array))
partition:(good idea adapt from introduction to algorithm)
def partition (dataList, pivot_index ):
"""这个函数只是为了让quick_sort()内部代码结构更紧凑而提取出来的(不是必须的)
Args:
dataList (list): [description]
pivot_index (Number): [description]
rightList (List): [description]
leftList (List): [description]
"""
size_of_dataList = len (dataList)
""" set two index variable to partition the sequence """
i = 0
j = size_of_dataList-1
""" save the pivot_element to be compared: """
pivot_element = dataList[pivot_index]
""" swap the pivot generated randomly,transform the familiar problem to solve """
dataList[pivot_index], dataList[0 ] = dataList[0 ], dataList[pivot_index]
"""
sequence_k:contains the elements<=pivot;
sequence_j:contains the elements>pivot;
k,j grow in two threads (we can know ,all of them grow continuosly separately)
"""
k = 0
for j in range (1 , size_of_dataList):
if dataList[j] <= pivot_element:
k += 1
dataList[k], dataList[j] = dataList[j], dataList[k]
""" insert(swap to) the pivot_element to proper location """
dataList[k],dataList[0 ]=dataList[0 ],dataList[k]
""" update the pivot_index: """
pivot_index = k
return pivot_index
python版2(非随机化&非置换版)
'''
Description:
Version: 2.0
Author: xuchaoxin
Date: 2021-03-07 17:06:52
LastEditors: xuchaoxin
LastEditTime: 2021-03-07 17:54:19
'''
def divide_zone (dataList, midElement, rightList, leftList ):
"""这个函数只是为了让quick_sort()内部代码结构更紧凑而提取出来的(不是必须的)
Args:
dataList (list): [description]
midElement (Number): [description]
rightList (List): [description]
leftList (List): [description]
"""
for num in dataList:
if num >= midElement:
rightList.append(num)
else :
leftList.append(num)
def quick_sort (dataList ):
"""利用递归来实现快速排序
是一种用空间换时间的方法(类似于归并法,快速法更简单些)
<<<!!!(定义好返回内容(功能)后,再开始递归函数的实现(具体编写依赖于我们对需求的清晰描述)
(先预设定义好功能,哪怕实现不了再回头调整,否则那一进行下去,毕竟递归函数内部要用到调用自己,函数的功能(特别是返回/计算结果)是什么样的很有必要明确)
Args:
dataList (list): 待排序的数列
Returns:
list: 排好序的数列!!!>>>
"""
if len (dataList) >= 2 :
""" 选取基准值,也可以选取第一个或最后一个元素(注意这里用地板除法)
这个基准值将要特地取出来,会用来连接左侧序列和右侧序列(也作为关节元素)
"""
midElement = dataList[len (dataList)//2 ]
""" # 定义基准值左右两侧的列表;随着递归的深入,leftList和rightList区可分配到的元素越来越少(问题规模不断降低)
对于不超过三个元素的某个序列certainList为参数的quick_sort(),"""
leftList, rightList = [], []
dataList.remove(midElement)
""" 逐个判断数列中的各个元素和基准值的大小关系,并放到对应侧的子序列中
可以考虑封装到一个函数中去"""
divide_zone(dataList, midElement, rightList, leftList)
""" 递归调用:对两侧子序列分别调用quick_sort()处理 """
return quick_sort(leftList) + [midElement] + quick_sort(rightList)
else :
return dataList
array = [2 ,3 ,5 ,7 ,1 ,4 ,6 ,15 ,5 ,2 ,7 ,9 ,10 ,15 ,9 ,17 ,12 ]
print (quick_sort(array))
C版
#include <stdio.h>
void quick_sort (int s[], int l, int r)
{
if (l < r)
{
int i = l, j = r, x = s[l];
while (i < j)
{
while (i < j && s[j] >= x)
j--;
if (i < j)
{
s[i] = s[j];
i++;
}
while (i < j && s[i] < x)
i++;
if (i < j)
s[j--] = s[i];
}
s[i] = x;
quick_sort(s, l, i - 1 );
quick_sort(s, i + 1 , r);
}
}
int main ()
{
int s[] = {5 , 4 , 6 , 7 , 7 , 1 };
int len = sizeof (s) / sizeof (int );
quick_sort(s, 0 , len - 1 );
for (int i = 0 ; i < len; i++)
{
printf ("%d " , s[i]);
}
}
C语言2
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#define ISHEAP 0
#define EASY 0
#define Read_From_Keyboard 0
#define MAX 100
void quickSort (int r[], int n) ;
int read (int * r)
{
#if ISHEAP
int i = 1 ;
#else
int i = 0 ;
#endif
while (scanf ("%d" , &r[i]) != EOF && i < MAX)
{
i++;
}
return i;
}
int main ()
{
#if Read_From_Keyboard
int r[MAX];
int n = read(r);
#else
#if ISHEAP
int heapr[] = {-99 , 19 ,15 ,13 ,1 ,6 ,7 ,0 ,3 ,2 ,4 };
#else
#if EASY
int r[] = { 10 ,9 ,8 ,7 ,7 ,5 ,4 ,4 ,2 ,1 };
int n = 10 ;
#else
int r[] = { 19 ,15 ,13 ,1 ,6 ,7 ,0 ,3 ,15 ,7 };
int n = 10 ;
#endif
#endif
#endif
printf ("testing:\n" );
quickSort(r, n);
#if (ISHEAP)
for (int i = 1 ; i <= n; i++) printf ("%d " , heapr[i]);
#else
for (int i = 0 ; i < n; i++) printf ("%d " , r[i]);
#endif
}
#define SWAP(x,y,t) {t = x;x = y;y = t;}
void swap (int * a, int * b)
{
int tmp = 0 ;
tmp = *a;
*a = *b;
*b = tmp;
}
int median3 (int r[], int left, int right)
{
int center = (left + right) / 2 ,
pivot;
if (r[center] <= r[left] && r[center] >= r[right] ||
r[center] >= r[left] && r[center] <= r[right])
pivot = center;
else if (r[left] <= r[center] && r[left] >= r[right] ||
r[left] >= r[center] && r[left] <= r[right])
pivot = left;
else pivot = right;
if (pivot != right)
swap(&r[pivot], &r[right]);
return r[right];
}
void quickSortImplement (int r[], int left, int right)
{
int pivot_index;
int saveLeft = left,
saveRight = right;
if (left < right)
{
int pivot = median3(r, left, right);
int tmp = 0 ;
while (left < right)
{
while (left < right && r[left] <= pivot)
{
left++;
}
SWAP(r[left], r[right], tmp);
while (left < right && r[right] >= pivot)
{
right--;
}
SWAP(r[left], r[right], tmp);
}
pivot_index = left;
quickSortImplement(r, saveLeft, pivot_index - 1 );
quickSortImplement(r, pivot_index + 1 , saveRight);
}
}
void quickSort (int r[], int n)
{
quickSortImplement(r, 0 , n - 1 );
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2022-03-08 windows上命令行@文件内容查看方案@行号显示@nlps@cat -n