`

implement qick sort myself

阅读更多
for spend some off hours, I decide to study some algorithm, the arithmetic of sort is a good choice. since sort algorithm bring forwand long time ago. but it's alway found the more efficienty method.

first, basic algorithm: bubble, insert, selection. just apply array to compare value between 2 number.
second, mergesort. apply recursive to separate split problem.
third, quick, find a middle value as "pivot", and split all value by pivot. and then recursive pivot, split, it's high-efficient sort algorithm.
other..., I will study it when I'm in dawdle. haha



package algorithm.sort;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

/**
 * quick sort algorithm<br />
 * 1. select a element as "pivot"<br />
 * 2. re-sort all element, all the element that less than pivot replace to front,<br />
 * all the element that more than pivot replace to the back,<br /> 
 * and set the pivot to middle, these action called by "partition" operate<br />
 * 3. recursive, process less array and greater array
 * @author YS
 */
public class QuickSort {

	/**
	 * implement by myself, implement the original concept whick get from wiki,<br />
	 * need extra space for store
	 * @param param
	 */
	public static void sort_implement_myself(Float[] param){
		System.out.println(Arrays.toString(param));
		int len=param.length;
		if(len<=1)
			return;

		//get pivot
		int middle=len/2;
		float pivot=param[middle];
		List<Float> l1=new LinkedList<Float>();
		List<Float> l2=new LinkedList<Float>();
		for(int i=0;i<len/2;i++){
			if(param[i]<pivot)
				l1.add(param[i]);
			else
				l2.add(param[i]);
		}
		for(int i=len/2+1;i<len;i++){
			if(param[i]<pivot)
				l1.add(param[i]);
			else
				l2.add(param[i]);
		}
		Float[] f1=l1.toArray(new Float[0]);
		Float[] f2=l2.toArray(new Float[0]);
		sort_implement_myself(f1);
		sort_implement_myself(f2);
		List<Float> l=new LinkedList<Float>();
		l.addAll(Arrays.asList(f1));
		l.add(pivot);
		l.addAll(Arrays.asList(f2));
		param=l.toArray(param);
	}

	/**
	 * quick sort with inplace
	 * @param param
	 * @param start
	 * @param last
	 */
	public static void sort_inplace_implement_myself(float[] param, int start, int last){
		float pivot=param[last];
		int i=start, j=last-1, middle=(last+start)/2;
		for(;i<middle;i++){
			if(param[i]>pivot){
				for(;j>=middle;j--){
					if(param[j]<param[i]){
						float tmp=param[i];
						param[i]=param[j];
						param[j]=tmp;
						break;
					}
				}
			}
		}
		if(param[last]<param[middle]){
			param[last]=param[middle];
			param[middle]=pivot;
		}
		int flag=last-start+1;

		if(flag>=3){
			sort_inplace_implement_myself(param, 0, middle);
			sort_inplace_implement_myself(param, middle+1, last);
		}
	}

	public static void main(String[] args) {
		float[] param={1, 8, 4, 3, 7, 5, 6, 8, 9, 2, 5, 3, 1, 8, 7, 3, 6};
//		float[] param={1,2,4,7,5,3};
		Float[] f=new Float[param.length];
		for(int i=0;i<param.length;i++){
			f[i]=Float.valueOf(param[i]);
		}
		sort_inplace_implement_myself(param, 0, param.length-1);
		System.out.println(Arrays.toString(param));
	}
}

分享到:
评论

相关推荐

    YOLOv5_和_DeepSORT_to_implement_ob_YOLOv5

    _YOLOv5_和_DeepSORT_to_implement_ob_YOLOv5_DeepSORT_传感器_This_repo_uses_YOLOv5_and_DeepSORT_to_implement_ob_yolov5_deepsort_tensorrt

    Java中extend与implement区别.doc

    Java 中 extend 与 implement 的区别 Java 语言中,extend 和 implement 是两个基本概念,它们之间的区别是非常重要的。extend 用于继承类,而 implement 用于实现接口。在 Java 中,不支持多重继承,但是可以使用...

    jcifs_java_implement_cifs

    jcifs_java_implement_cifs jcifs_java_implement_cifs jcifs_java_implement_cifs jcifs_java_implement_cifs jcifs_java_implement_cifs jcifs_java_implement_cifs

    C Interface and Implement

    While most C programmers use APIs and the libraries that implement them in almost every application they write, relatively few programmers create and disseminate new, widely applicable APIs....

    eclipse implementor 插件

    Eclipse Implementor插件是专为Eclipse IDE设计的一款实用工具,它主要目的是为了帮助开发者更高效地实现接口或抽象类的方法。在Java编程中,当我们需要为一个接口或抽象类提供具体实现时,手动编写这些方法可能会...

    7.4.1 Packet Tracer - Implement DHCPv4

    7.4.1 Packet Tracer - Implement DHCPv4 Cisco Packet Tracer 思科模拟器 正确答案文件 由于程序问题无法保存激活DHCP端口配置 另加满分步骤截图 可直接上交正确答案文件 本答案版权归mewhaku所有,严禁再次...

    6.4.1 Packet Tracer - Implement Etherchannel

    6.4.1 Packet Tracer - Implement Etherchannel Cisco Packet Tracer 思科模拟器 正确答案文件 可直接上交正确答案文件 本答案版权归mewhaku所有,严禁再次转载!!! Copyright @mewhaku 2022 All Rights ...

    1.6.1 Packet Tracer - Implement a Small Network

    1.6.1 Packet Tracer - Implement a Small Network Cisco Packet Tracer 思科模拟器 正确答案文件 可直接上交正确答案文件 本答案版权归mewhaku所有,严禁再次转载!!! Copyright @mewhaku 2022 All Rights ...

    3.6.1 Packet Tracer - Implement VLANs and Trunking

    3.6.1 Packet Tracer - Implement VLANs and Trunking Cisco Packet Tracer 思科模拟器 正确答案文件 可直接上交正确答案文件 本答案版权归mewhaku所有,严禁再次转载!!! Copyright @mewhaku 2022 All Rights ...

    Practical Convolutional Neural Networks Implement advanced d l models using Py

    By the end of this book, you should be ready to implement advanced, effective and efficient CNN models at your professional project or personal initiatives by working on complex image and video ...

    C++ file implement 英文习题

    C++ 针对 文件 操作 英文习题 Read the content of a file, and output to the screen. Every time after output 10 lines, ask users if they want to stop the program.

    odoo 10 implement. pdf

    非完美pdf版本,但是可以看 非完美pdf版本,但是可以看

    Design and Implement BI

    BI Design and implementation, BI Design and implementation, BI Design and implementation, BI Design and implementation,

    Teradata Cookbook Over 85 recipes to implement efficient data warehousing epub

    Teradata Cookbook Over 85 recipes to implement efficient data warehousing solutions 英文epub 本资源转载自网络,如有侵权,请联系上传者或csdn删除 查看此书详细信息请在美国亚马逊官网搜索此书

    SELinux_System_Administration_Implement_mandatory_access_control

    SELinux_System_Administration_Implement_mandatory_access_control.epub

    R Data Mining Implement data mining techniques through practical use cases mobi

    R Data Mining Implement data mining techniques through practical use cases and real world datasets 英文mobi 本资源转载自网络,如有侵权,请联系上传者或csdn删除 查看此书详细信息请在美国亚马逊官网搜索...

    hadoop join implement

    ### Hadoop Join Implementation:关键技术与优化策略 #### 摘要 在大数据处理领域,Hadoop作为主流的大规模数据处理框架之一,其MapReduce模型在并行数据处理方面展现出巨大优势。然而,对于数据间的连接操作(即...

    Implement with Class and Collection a List Collection with a

    标题 "Implement with Class and Collection a List Collection with a" 暗示了我们需要讨论如何使用类(Class)和集合(Collection)来实现一个列表集合,并具备添加(add)和移除(remove)数据元素的功能。...

    Planing to Implement Service Management.pdf

    通过以上知识点的梳理,我们可以看出,《Planing to Implement Service Management》这本书提供了一个全面而深入的指南,帮助读者理解和实施服务管理的原则和实践。无论是对于刚接触该领域的初学者还是寻求进一步...

    Implement deep networks for digit classification Exercise 权值矩阵

    标题中的"Implement deep networks for digit classification Exercise 权值矩阵"指的是使用深度网络进行数字分类的实践练习,重点在于权值矩阵。在这个练习中,我们通常会构建一个多层神经网络,如深度信念网络...

Global site tag (gtag.js) - Google Analytics