sort

python实现快排

旧时模样 提交于 2020-02-11 09:56:52
快速排序 PYTHON 不那么好看的快排,参考 博客 def quick_sort(arr): if len(arr) <= 1: return arr else: base = arr[0] less = [v for v in arr[1:] if v<=base] more = [v for v in arr[1:] if v>base] return quick_sort(less) + [base] + quick_sort(more) 好看一点的快排,参考 博客 def quick_sort(arr,l,r): if l < r: q = partition(arr,l,r) quick_sort(arr,l,q-1) quick_sort(arr,q+1,r) def partion(arr,l,r): x = arr[r] i = l - 1 for j in (l,r): if arr[j] <= x: i += 1 arr[i], arr[j] = arr[j], arr[i] arr[i+1], arr[r] = arr[r], arr[i+1] return i+1 来源: https://www.cnblogs.com/curtisxiao/p/11184773.html

c#快速排序

蓝咒 提交于 2020-02-11 09:55:21
using System; /* 作者:朱剑 描写:C#实现快速排序算法 创建日期:2006/05/08 */ namespace ConsoleApplication1 { class DataStructDemo { static void swap( ref int a, ref int b) { int temp; temp = a; a = b; b = temp; } static void sort( int [] arr, int left, int right) { int i,j,s; if (left < right) { i = left - 1 ; j = right + 1 ; s = arr[(i + j) / 2 ]; while ( true ) { while (arr[ ++ i] < s); while (arr[ -- j] > s); if (i >= j) break ; swap( ref arr[i], ref arr[j]); } sort(arr,left,i - 1 ); sort(arr,j + 1 ,right); } } [STAThread] static void Main( string [] args) { int [] arr = { 2 , 4 , 65 , 76 , 87 , 90 , 56 , 89 ,

各类排序算法java的实现

夙愿已清 提交于 2020-02-11 09:08:47
插入排序: package org.rut.util.algorithm.support; import org.rut.util.algorithm.SortUtil; /** * @author treeroot * @since 2006-2-2 * @version 1.0 */ public class InsertSort implements SortUtil.Sort{ /* (non-Javadoc) * @see org.rut.util.algorithm.SortUtil.Sort#sort(int[]) */ public void sort( int [] data) { int temp; for ( int i = 1 ;i < data.length;i ++ ){ for ( int j = i;(j > 0 ) && (data[j] < data[j - 1 ]);j -- ){ SortUtil.swap(data,j,j - 1 ); } } } } 冒泡排序: package org.rut.util.algorithm.support; import org.rut.util.algorithm.SortUtil; /** * @author treeroot * @since 2006-2-2 * @version 1.0 */

Java实现各种排序算法

北战南征 提交于 2020-02-11 08:43:48
曾经学数据结构的时候,各种排序练的很熟,但是想过用Java怎么实现吗,以下给出来给你看看,当然闲着就当学习数据结构了,因为jdk提供的工具足够你应付所有事情。 插入排序: package org.rut.util.algorithm.support; imp ort org.rut.util.algorithm.SortUtil; public class InsertSort implements SortUtil.Sort{ public void sort(int[] da ta) { int temp; for(int i=1;i<da ta.length;i++){ for(int j=i;(j>0)&&(da ta[j]<da ta[j-1]);j--){ SortUtil.swap(da ta,j,j-1); } } } } 冒泡排序: package org.rut.util.algorithm.support; imp ort org.rut.util.algorithm.SortUtil; public class BubbleSort implements SortUtil.Sort{ public void sort(int[] da ta) { int temp; for(int i=0;i<da ta.length;i++){ for(int j=da

各种排序算法

自作多情 提交于 2020-02-11 08:43:21
只有代码, package 各种排序算法; public class Test { public static void main(String[] args) { // TODO Auto-generated method stub int[] a = {3,4,2,33,55,66,234,234,653,43,6}; long s = System.currentTimeMillis(); int[]temp = new int[a.length]; //insert_sort(a); shell_sort(a); System.out.println(System.currentTimeMillis()-s); for(int i = 0; i<a.length; i++) { System.out.print(a[i]+" "); } } // 快排 public static void quick_sort(int[] a,int start,int end) { if(start>=end) return; int i = start; int j = end; int key = a[start]; while(i<j) { while(i<j&&a[j]>=key) { j--; } a[i] = a[j]; while(i<j&&a[i]<=key) { i++

几种常见的排序算法

霸气de小男生 提交于 2020-02-09 21:58:10
一.选择排序   在待排序的一组数据中,选出最小(最大)的一个数与第一个位置的数交换,然后在剩下的数中,再找最小(最大)的数与第二个位置的数交换位置,依次类推,直到第N-1个元素与第N个元素交换位置,选择排序结束。 import edu.princeton.cs.algs4.StdIn; import edu.princeton.cs.algs4.StdOut; import java.util.Comparator; public class Selection { private Selection() { } //排序 public static void sort(Comparable[] a) { int n = a.length; for (int i = 0; i < n; i++) { int min = i; for (int j = i+1; j < n; j++) { if (less(a[j], a[min])) min = j; } exch(a, i, min); assert isSorted(a, 0, i); } assert isSorted(a); } //使用比较器按升序重新排列数组 public static void sort(Object[] a, Comparator comparator) { int n = a.length;

leetcode80. Remove Duplicates from Sorted Array II

吃可爱长大的小学妹 提交于 2020-02-09 18:19:11
题目描述 Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twice and return the new length. Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory. Example 1: Given nums = [1,1,1,2,2,3], Your function should return length = 5, with the first five elements of nums being 1, 1, 2, 2 and 3 respectively. It doesn’t matter what you leave beyond the returned length. 来源:力扣(LeetCode) 链接: https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array-ii 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

容器list

血红的双手。 提交于 2020-02-09 10:18:33
//双向链表,比较节省内存,每次扩充只扩充一个单元//由属于自己的sort,为了提高运行的效率尽量不要采用全局的sort#include <iostream> #include<list> using namespace std; int main() { list<int> li; li.push_back(3); li.push_front(2); li.emplace_front(1);//优化改进的push_front; li.emplace_back(4); li.insert(++li.begin(),9);//在具体位置处添加对应的元素 for(auto x:li) cout<<x; return 0; } 来源: https://www.cnblogs.com/zmachine/p/12286288.html

ECSHOP程序SEO完全优化

和自甴很熟 提交于 2020-02-09 07:31:31
一、完全自定义页面titile,完全抛弃Ecshop定义的页面title格式:[产品名称]_[分类名]_[网店名称]-Powered by ECShop 1、分析:大家都知道,titile,kewords,description在SEO中的基础性和重要性,但是ECshop官方给出的titile实现方法会出现很多类似或相同的页面title,另外还有部分页面无法自定义kewords和 description。在此本人给出了完全自定义的方法,其实方法很简单的,只要稍微懂得一点点代码的都会。 2、修改包括:品牌页,商品页,商品分类页,文章页,文章分类页 3、修改涉及的页面: 品牌页(增加title,keywords和description):brand.php、admin\brand.php、 admin\templates\brand_info.htm 商品页(增加title):goods.php、admin\goods.php、 admin\templates\goods_info.htm 商品分类页(增加title):category.php、admin\category.php、 admin\templates\category_info.htm 文章页面(增加title):article.php、admin\article.php、 admin\templates\article

调用qsort自定义快速排序

你说的曾经没有我的故事 提交于 2020-02-08 18:53:21
void qsort ( void * base , int nelem , unsigned int width , int ( * mycompare ) ( const void * , const void * ) ) ; base:待排序数组的起始地址; nelem:待排序数组的元素个数; width:待排序数组的每个元素的大小(sizeof(T) (T代表变量名,如int,double,等)) 最后就是自定义的比较函数了。 排序就是一个不断比较并交换位置的过程。qsort函数在执行期间通过*mycompare指针调用比较函数,调用时将要比较的两个元素的地址传给比较函数,然后根据比较函数的返回值判断这两个元素哪个更应该排在前面。 int 比较函数名(const void e1,const void e2); 比较函数的编写规则: 1.如果 e1应该在 e2前面,返回值应该为负整数; 2.如果 e1应该在 e2后面,返回值应该为正整数; 3.如果 e1与 e2谁在前谁在后都可,那么函数返回值应该为0。 举个例子:将23,47,134,9,435这几个数,按个位数从小到大排序。 # include <iostream> # include <cstdio> # include <cstring> # include <cmath> # include <ctype.h> #