用python写个快排

快排过程比较简单就直接上代码了:

 1 #!/usr/bin/python3
 2
 3 def quik_sort(L, left, right):
 4     if left <= right:
 5         key = L[left]
 6         i = left
 7         j = right
 8         while i < j:
 9             while i < j and key <= L[j]:
10                 j -= 1
11             L[i] = L[j]
12             while i < j and L[i] <= key:
13                 i += 1
14             L[j] = L[i]
15         L[i] = key
16         quik_sort(L, left, i - 1)
17         quik_sort(L, i + 1, right)
18
19
20
21 a = list(map(int, input().split()))
22
23 quik_sort(a, 0, len(a) - 1)
24
25 for x in a:
26     print(x, end = ‘ ‘)
27 print()

注意:python 中的数字类型是不可变的,因此 x++ 并不会让 x 的值加 1

原文地址:https://www.cnblogs.com/geloutingyu/p/8747440.html

时间: 2024-08-04 12:31:01

用python写个快排的相关文章

python 版 quicksort 快排

今天看了下苹果xml 解析,写了个小demo 心想还是 在博客上写点东西吧,毕竟很久很久都没有上来了 先上个效果图把 接下来 看下 工程目录图吧 本demo 分两种解析模式,一是苹果自带的, 首先先看下苹果自带的吧,工程文件为 NoteXMLParser 文件 ,另一种解析模式 是 NotesTBXMLParser文件 NoteXMLParser.h 文件代码如下 : // // NoteXMLParser.h // TestXML // // Created by choni on 14-5-

今天天气很好,写个快排练练手

最近有些懒,好久没写代码了,现在写个快排练练手吧. public class QucikSort { //此处交换两个数 public static void swap(int a[],int low,int high) { int temp=a[low]; a[low]=a[high]; a[high]=temp; } //分区,分成两部分 public static int partion(int a[],int low,int high) { swap(a,low,high); int c

python 冒泡和快排,不多说【无聊】

1 #-*-coding:utf8-*- 2 import random 3 a=[] 4 b=[] 5 def init_array(): 6 for i in range(10000): 7 v = random.randint(1,10000) 8 a.append(v) 9 b.append(v) 10 11 #冒泡 12 def sort_array(a): 13 for i in range(len(a)): 14 for j in range(i+1,len(a)): 15 if

Python的快排应有的样子

快排算法 ? 简单来说就是定一个位置然后,然后把比它小的数放左边,比他大的数放右边,这显然是一个递归的定义,根据这个思路很容易可以写出快排的代码 ? 快排是我学ACM路上第一个让我记住的代码,印象很深刻,以前学的是Pascal,写这个要写好长一串,但是因为和归并排序比起来还算短的,也就背下来了.好奇的我点开百科看python的快排代码,就看到了如下代码: #quick sort def quickSort(L, low, high): i = low j = high if i >= j: re

用js写了个快排,还有优化的余地

看了一天别人的代码,换换思路,写个快排. 直接上代码,低估了,原理一直记得,本以为10分钟能搞定,但出现很多小bug,整了20多分钟. /** * Created by cdpmac on 15/5/15. */ var arr = [5, 3, 7, 4, 1, 9, 8, 6, 2]; quickSort(arr,0,arr.length-1); console.log(arr); function quickSort(arr,left,right){ if( left>=right){ r

快排(再次实现)

刚刚CVTE笔试,想写个快排,居然没写出来.写出来.出来.来!!!简直了,打死自己 再实现一遍吧! #include <iostream> #include <stdio.h> void qsort(int first,int last,int* array,size_t size) { int left = first; int right = last; if (first >= last) return; int key = array[first]; int mid 

hdu1157 快排

题目:http://acm.hdu.edu.cn/showproblem.php?pid=1157 大意:排序,取中间数. PS:1.自己实现了下快排函数,也可以使用#include<algorithm>下的sort(a,a+n);函数,默认升序,若要降序or结构体排序可以增加第三个参数,声明排序规则. 2.在写这个快排的时候出现了很多问题,花了比较多的时间,对自己很不满意. 3.在这个while循环里写自减时,应该是j=high+1(分(low~p-1)和(p+1~high)),若不进行hi

伤心的快排

#include<iostream> using namespace std; ///快排,百度数据挖掘,我面到终面 ///他们部门经理过来面我 ///数据挖掘的东西,兵来将挡,水来土掩 ////讲完项目于对互联网的理解 ///他说,最后写个快排吧~~~~ ////FUCK,我没写出来 ////嚓嚓嚓嚓嚓 int AdjustArray(int* a,int l,int r) { int x = a[l]; while(l<r) { while(l<r && a[r

Python实现排序(冒泡、快排、归并)

Thomas H.Cormen 的<算法导论>上介绍的几个经典排序算法的Python实现. 1.冒泡排序: 简单的两重循环遍历,使最小(最大)的值不断地往上升(下沉)而实现的排序,算法时间为O(n2). 代码如下: 1 def up_sort(a): 2 # 冒泡排序 3 4 a_length = len(a) 5 while True: 6 i = 0 7 j = 1 8 9 while True: 10 if a[i] > a[j]: 11 a[i], a[j] = a[j], a[