package com.kneel.core.utils;
/**
* sort is the time:space.
*
* 1. if you want speed, then you need more space, you can use multiple threads to process multiple parts at one time.
* 2. if you have limit space, then you can process a part of it one by one.
*
* there is no best sort, just have fit of your business logic sort.
*
* @author e557400
*
*/
public class SortUtils {
/**
* bubble up
*
* 1. compare with current value and next value.
* 2. if current value big then next value, change position.
*
* Example: i need to compare with (n-1), and put the min Number to header
* i+1 need to compare with (n-2), and put the second min Number to header.
* ...
* n-2 need to compare with 1, and put the last min Number to header.
*
*
* NOTE: compare(n*(n-1)/2), no move.
* compare(n*(n-1)/2), every compare need to move, move(n*(n-1)/2)
*
* time: O(n~2) bad situation
* space: O(1)
*
* every time sort should be load all datas to memory.
*
* Exp
*o. 60--38--15--75--51--
*1. 38--60--15--75--51--
*2. 15--60--38--75--51--
*3. 15--38--60--75--51--
*4. 15--38--51--75--60--
*5. 15--38--51--60--75--
* @param x
* @return
*/
public static <T extends Comparable<T>> void bubbleUp(T[] array,boolean ascend) {
for (int i = 0; i < array.length; i++) {
for (int j = i + 1; j < array.length; j++) {
int compare = array[i].compareTo(array[j]);
if (compare != 0 && compare>0 ==ascend) {
T temp = array[j];
array[j] = array[i];
array[i] = temp;
}
}
}
}
/**
* Select up
*
* this sort is the upgrade of bubble up, i agree.
*
* 1. iterator array, i is the index of the sort, find the min of [i...n-1], then change position of the i.
* 2. case of every iterator will be choose min process, so call it select sort.
*
* NOTE: compare(n*(n-1)/2), no move.
* compare(n*(n-1)/2), every compare need to move, move(n)
*
*
* Exp
*o. 60--38--15--75--51--
*1. 15--38--60--75--51--
*2. 15--38--60--75--51--
*3. 15--38--51--75--60--
*4. 15--38--51--60--75--
*5. 15--38--51--60--75-
*
* @param array
*/
public static <T extends Comparable<T>> void selectUp(T[] array,boolean ascend) {
int len = array.length;
for (int i = 0; i < len; i++) {
int selected = i;
for (int j = i + 1; j < len; j++) {
int compare = array[selected].compareTo(array[j]);
if (compare != 0 && compare > 0 ==ascend) {
selected = j;
}
}
T temp = array[selected];
array[selected] = array[i];
array[i] = temp;
}
}
/**
* Insert Up
*
* almost element is sorted
*
* 1. we set index i 's left is sorted, to find the i the index.
* 2. we will be compare i's left's data one by one, if previous value big then current value, insert this index.
*
*
* NOTE: compare(n*(n-1)/2), no move.
* compare(n*(n-1)/2), every compare need to move, move(n)
*
* Exp
*o. 60--38--15--75--51--
*1. 38--60--15--75--51--
*2. 15--38--60--75--51--
*3. 15--38--60--75--51--
*4. 15--38--51--60--75--
*5. 15--38--51--60--75--
*
* @param array
*/
public static <T extends Comparable<T>> void insertUp(T[] array,boolean ascend){
for(int i=1; i<array.length; i++){
T toInsert = array[i];
int j=i;
for(;j>0;j--){
int compare = toInsert.compareTo(array[j-1]);
if(compare == 0 || compare>0 == ascend){//next value big then previous value.find break.
break;
}
array[j]=array[j-1];//next value less then previous value,
}
array[j] = toInsert;//we find the position.
}
}
/**
* Shell Up
*
* this sort is the upgrade of insert up, i agree. [group is big, fix need to move so many postions]
*
* first group all data as multiple parts, then sort one group by one group, final collect all together.
*
* 1. we set index i 's left is sorted, to find the i the index.
* 2. we will be compare i's left's data one by one, if previous value big then current value, insert this index.
*
*
* NOTE: compare(n*(n-1)/2), no move.
* compare(n*(n-1)/2), every compare need to move, move(n)
*
* @param array
*/
public static <T extends Comparable<T>> void shellUp(T[] array,boolean ascend){
int length = array.length;
int gap = 1;
while (gap < length / 3) {
gap = gap * 3 + 1;
}
while (gap >= 1) {
for (int i = gap; i < length; i++) {
T next = array[i];
int j = i;
while (j >= gap) {
int compare = next.compareTo(array[j - gap]);
// already find its position
if (compare == 0 || compare > 0 ==ascend) {
break;
}
array[j] = array[j - gap];
j -= gap;
}
if (j != i) {
array[j] = next;
}
}
gap /= 3;
}
}
/**
* quick up
*
* C.A.R.Hoare generator in 1962. split sort data as two part, one part of the data is less than other part.
* recursive this round,end with two element, one is less than other.
*
* 1. get one of the list as the center (example first one)
* 2. if the element lesser then it, put to left position.(high-- as index)
* 3. if the element bigger then it, put to right position.(low++ as index)
* 4. re-pick one of the left list as the center(first one)[2,3]
* 5. re-pick one of the right list as the center(first one)[2,3]
* 6. until every table have only one element, it is end.
*
* you can think as find first people(with NO) as center, make two middle people, one is at end of the list,
* one is at begin of the list
* 1. first call first people to go temp.
* 2. right one find people less then x[0], if find x[a], call him to x[0].[high:a]
* 3. left one find people bigger then x[0], if find x[b], call him to x[a].[low:b]
* 4. if a<b, right one continue, then left one, until a<b. skip out.
* 5. set a[l] to temp. then the list is x[0...l-1] is less then x[l+1,hight], middel is x[l].
* 6. condition round. sort by multiple part.
*
* NOTE: compare(n*(n-1)/2), no move.
* compare(n*(n-1)/2), every compare need to move, move(n)
*
*o. 60--38--15--75--51--
*1. 51--38--15--60--75--
*2. 15--38--51--60--75--
*3. 15--38--51--60--75--
*4. 15--38--51--60--75--
*5. 15--38--51--60--75--
*
* @param x
* @return
*/
public static int[] quickUp(int [] x){
if(x.length>0){
quicksort(x,0,x.length-1);
}
return x;
}
public static void quicksort(int [] x,int low,int high){
if(low<high){
int middle=getmiddle(x,low,high);
quicksort(x,0,middle-1);
quicksort(x,middle+1,high);
}
}
public static int getmiddle(int [] x,int low,int high){
int temp=x[low];// pick first element as middle.
while(low<high){// all iterator.
while(low<high&&x[high]>=temp){// if element bigger then the middle, no change.
high--;
}
x[low]=x[high];//(x[low]>x[high]) set the x[low] as x[high]
while(low<high&&x[low]<=temp){// if element lesser then the middle, no change.
low++;
}
x[high]=x[low];//(x[low]>x[high])
}
x[low]=temp;// put the middle value as x[low]
return low;
}
}
分享到:
相关推荐
Java 排序算法使用及场景说明 本文档主要介绍了 Java 排序算法的使用和场景说明,包括了五个实践场景的解决方案。 Scenario 1: 找出两个文件共同的 URL 在这个场景中,我们有两个文件 a 和 b,每个文件中存放了 ...
Java排序算法大全是一份专为Java开发者准备的学习资源,涵盖了各种经典的排序算法,旨在帮助初学者和有经验的程序员深入理解排序的原理和实现。排序是计算机科学中的基础且重要的概念,它在数据处理、数据库操作、...
Java排序算法实现 Java排序算法实现 Java排序算法实现
java排序算法java排序算法插入选择冒泡java排序算法插入选择冒泡
在Java编程语言中,排序算法是数据结构与算法学习中的重要组成部分。本实验通过生成大量随机数并写入文件,然后使用四种不同的排序算法进行排序,以比较它们的效率。以下是对这四种排序算法的详细解释: 1. **冒泡...
【Java排序算法详细整理】 在计算机科学中,排序算法是用于对一组数据进行排列的算法。在Java中,实现各种排序算法有助于理解数据结构和算法的原理,同时也能提高编程能力。以下是对Java中常见的几种排序算法的详细...
这个"Java排序算法包"提供了对多种排序算法的支持,并且允许用户根据自己的需求自定义比较条件,使得排序功能更加灵活。 1. **排序算法基础**: - 排序是指将一组数据按照特定的顺序进行排列的过程。常见的排序...
Java排序算法涉及了多种方法,用于组织数组或集合中的元素,使其按照特定顺序排列。以下是对这些算法的详细解释: 1. **冒泡排序(Bubble Sort)** 冒泡排序是一种简单直观的排序算法,它重复地遍历待排序的数列,一...
代码中列举了java常见的排序算法,并备有简单的注释信息,对于初级开发人员可供参考。
Java排序算法涉及了多种方法,每种都有其特定的适用场景和性能特点。本篇将深入探讨几种常见的Java排序算法,包括冒泡排序、插入排序、选择排序、快速排序、归并排序、堆排序以及TimSort等。 1. **冒泡排序**: ...
这个名为"java排序算法-大全.rar"的压缩包文件显然包含了多种Java实现的排序算法,这对于我们理解和掌握这些算法至关重要。 首先,让我们从标签提及的两个经典排序算法开始:冒泡排序和折半排序。 1. **冒泡排序**...
本资源提供了丰富的Java排序算法的演示源码,注解详尽,有助于理解和学习。 1. **冒泡排序(Bubble Sort)** 冒泡排序是最基础的排序算法之一,通过不断地交换相邻的不正确顺序的元素来逐步完成排序。源码中应该...
Java排序算法是编程面试和笔试中常见的考察点,掌握这些算法对于提升编程能力和解决实际问题至关重要。本篇文章将深入探讨几种主要的Java排序算法及其特点。 1. **插入排序** - **直接插入排序**:将每个元素依次...
Java排序算法汇总大全 在计算机科学中,排序算法是用于对数据序列进行排列的算法,以便根据特定标准对其进行组织。本文将详细介绍Java中常见的几种排序算法,并提供它们的基本原理、性能分析以及适用场景。 1. ...
本文将详细探讨标题所提及的几种排序算法:合并排序、插入排序、希尔排序、快速排序、冒泡排序以及桶排序,并结合Java语言的实现进行解析。 1. **合并排序(Merge Sort)**: 合并排序是一种基于分治策略的排序算法...
Java排序算法是编程领域中的重要知识点,特别是在处理大量数据时,高效的排序算法能显著提升程序性能。本资源包含了Java实现的常见排序算法集合,对于学习和理解这些算法有着极大的帮助。 1. 冒泡排序(Bubble Sort...
【Java排序算法集合】 在Java编程中,排序算法是数据结构和算法中不可或缺的一部分,它用于将一组数据按照特定的顺序排列。常见的排序算法包括选择排序、冒泡排序和插入排序,下面我们将逐一探讨这些算法的基本思想...