㈠ 排序都有哪几种方法用java实现一个快速排序。
排序的方法有:插入排序(直接插入排序、希尔排序),交换排序(冒泡排序、快速排序),选择排序(直接选择排序、堆排序),归并排序,分配排序(箱排序、基数排序)
快速排序的伪代码。
/ /使用快速排序方法对a[ 0 :n- 1 ]排序
从a[ 0 :n- 1 ]中选择一个元素作为m i d d l e,该元素为支点
把余下的元素分割为两段left 和r i g h t,使得l e f t中的元素都小于等于支点,而right 中的元素都大于等于支点
递归地使用快速排序方法对left 进行排序
递归地使用快速排序方法对right 进行排序
所得结果为l e f t + m i d d l e + r i g h t
㈡ JAVA实现插入排序
public class Test {
public static void main(String[] args) {
int[] source = { 1, 3, 2, 5, 12, 3, 123, 23, 2, 541, 1, 76, 76 };
Test test = new Test();
test.printArray(source);
test.insertSort(source);
test.printArray(source);
}
public void insertSort(int[] source) {
for (int i = 1; i < source.length; i++) {
for (int j = i; (j > 0) && (source[j] < source[j - 1]); j--) {
swap(source, j, j - 1);
}
}
printArray(source);// 输出插入排序后的数组值
}
private void swap(int[] source, int x, int y) {
int temp = source[x];
source[x] = source[y];
source[y] = temp;
}
public void printArray(int[] source) {
for (int i : source) {
System.out.print(i + " ");
}
System.out.println();
}
}
㈢ 请给出java几种排序方法
java常见的排序分为:
1 插入类排序
主要就是对于一个已经有序的序列中,插入一个新的记录。它包括:直接插入排序,折半插入排序和希尔排序
2 交换类排序
这类排序的核心就是每次比较都要“交换”,在每一趟排序都会两两发生一系列的“交换”排序,但是每一趟排序都会让一个记录排序到它的最终位置上。它包括:起泡排序,快速排序
3 选择类排序
每一趟排序都从一系列数据中选择一个最大或最小的记录,将它放置到第一个或最后一个为位置交换,只有在选择后才交换,比起交换类排序,减少了交换记录的时间。属于它的排序:简单选择排序,堆排序
4 归并类排序
将两个或两个以上的有序序列合并成一个新的序列
5 基数排序
主要基于多个关键字排序的。
下面针对上面所述的算法,讲解一些常用的java代码写的算法
二 插入类排序之直接插入排序
直接插入排序,一般对于已经有序的队列排序效果好。
基本思想:每趟将一个待排序的关键字按照大小插入到已经排序好的位置上。
算法思路,从后往前先找到要插入的位置,如果小于则就交换,将元素向后移动,将要插入数据插入该位置即可。时间复杂度为O(n2),空间复杂度为O(1)
package sort.algorithm;
public class DirectInsertSort {
public static void main(String[] args) {
// TODO Auto-generated method stub
int data[] = { 2, 6, 10, 3, 9, 80, 1, 16, 27, 20 };
int temp, j;
for (int i = 1; i < data.length; i++) {
temp = data[i];
j = i - 1;
// 每次比较都是对于已经有序的
while (j >= 0 && data[j] > temp) {
data[j + 1] = data[j];
j--;
}
data[j + 1] = temp;
}
// 输出排序好的数据
for (int k = 0; k < data.length; k++) {
System.out.print(data[k] + " ");
}
}
}
三 插入类排序之折半插入排序(二分法排序)
条件:在一个已经有序的队列中,插入一个新的元素
折半插入排序记录的比较次数与初始序列无关
思想:折半插入就是首先将队列中取最小位置low和最大位置high,然后算出中间位置mid
将中间位置mid与待插入的数据data进行比较,
如果mid大于data,则就表示插入的数据在mid的左边,high=mid-1;
如果mid小于data,则就表示插入的数据在mid的右边,low=mid+1
最后整体进行右移操作。
时间复杂度O(n2),空间复杂度O(1)
package sort.algorithm;
//折半插入排序
public class HalfInsertSort {
public static void main(String[] args) {
int data[] = { 2, 6, 10, 3, 9, 80, 1, 16, 27, 20 };
// 存放临时要插入的元素数据
int temp;
int low, mid, high;
for (int i = 1; i < data.length; i++) {
temp = data[i];
// 在待插入排序的序号之前进行折半插入
low = 0;
high = i - 1;
while (low <= high) {
mid = (low + high) / 2;
if (temp < data[mid])
high = mid - 1;
else
// low=high的时候也就是找到了要插入的位置,
// 此时进入循环中,将low加1,则就是要插入的位置了
low = mid + 1;
}
// 找到了要插入的位置,从该位置一直到插入数据的位置之间数据向后移动
for (int j = i; j >= low + 1; j--)
data[j] = data[j - 1];
// low已经代表了要插入的位置了
data[low] = temp;
}
for (int k = 0; k < data.length; k++) {
System.out.print(data[k] + " ");
}
}
}
四 插入类排序之希尔排序
希尔排序,也叫缩小增量排序,目的就是尽可能的减少交换次数,每一个组内最后都是有序的。
将待续按照某一种规则分为几个子序列,不断缩小规则,最后用一个直接插入排序合成
空间复杂度为O(1),时间复杂度为O(nlog2n)
算法先将要排序的一组数按某个增量d(n/2,n为要排序数的个数)分成若干组,每组中记录的下标相差d.对每组中全部元素进行直接插入排序,然后再用一个较小的增量(d/2)对它进行分组,在每组中再进行直接插入排序。当增量减到1时,进行直接插入排序后,排序完成。
package sort.algorithm;
public class ShellSort {
public static void main(String[] args) {
int a[] = { 1, 54, 6, 3, 78, 34, 12, 45, 56, 100 };
double d1 = a.length;
int temp = 0;
while (true)
{
//利用这个在将组内倍数减小
//这里依次为5,3,2,1
d1 = Math.ceil(d1 / 2);
//d为增量每个分组之间索引的增量
int d = (int) d1;
//每个分组内部排序
for (int x = 0; x < d; x++)
{
//组内利用直接插入排序
for (int i = x + d; i < a.length; i += d) {
int j = i - d;
temp = a[i];
for (; j >= 0 && temp < a[j]; j -= d) {
a[j + d] = a[j];
}
a[j + d] = temp;
}
}
if (d == 1)
break;
}
for (int i = 0; i < a.length; i++)
System.out.print(a[i]+" ");
}
}
五 交换类排序之冒泡排序
交换类排序核心就是每次比较都要进行交换
冒泡排序:是一种交换排序
每一趟比较相邻的元素,较若大小不同则就会发生交换,每一趟排序都能将一个元素放到它最终的位置!每一趟就进行比较。
时间复杂度O(n2),空间复杂度O(1)
package sort.algorithm;
//冒泡排序:是一种交换排序
public class BubbleSort {
// 按照递增顺序排序
public static void main(String[] args) {
// TODO Auto-generated method stub
int data[] = { 2, 6, 10, 3, 9, 80, 1, 16, 27, 20, 13, 100, 37, 16 };
int temp = 0;
// 排序的比较趟数,每一趟都会将剩余最大数放在最后面
for (int i = 0; i < data.length - 1; i++) {
// 每一趟从开始进行比较,将该元素与其余的元素进行比较
for (int j = 0; j < data.length - 1; j++) {
if (data[j] > data[j + 1]) {
temp = data[j];
data[j] = data[j + 1];
data[j + 1] = temp;
}
}
}
for (int i = 0; i < data.length; i++)
System.out.print(data[i] + " ");
}
}
㈣ Java插入排序
Java程序入口是main函数,
而main函数的参数必须是String[] args;
所以你要把函数名mian改成其他如sort,并新写一个main函数来调用他。
public static void main(String[] args){
}
㈤ Java 直接插入排序法
比如数组[3,2,1,5]
这段处理就返回[1,2,3,5]
它的处理是从第二位开始依次跟前边的比,比前边的小就往前移动。
也就是[3,2,1,5]
[2,3,1,5]
[1,2,3,5]
(int j = i - 1; j >= 0 && temp < array[j]; j--)
i是这次处理的下标,第一次是1,第二次是2,第三次是3,对应上边原数组里的2,1,5
处理开始时把下标i对应的值存在temp里,j表示的是i前边的下标,temp < array[j]的时候说明i下标的值比前边的小,所以把小的值拿到前边去。
这么看是很抽象的,自己在本上写的数组,画一下处理过程会更有助于你理解。
㈥ java 直接插入排序的例子
public void insertSort(int[] data)
{
int length = data.length();//获取数组长度
for(int i=1;i<length;i++)
{
int tmp = data[i];
if(tmp<data[i-1])
{
int j = i-1;
for(;j>=0&&data[j]>tmp;j--)
{
data[j+1] = data[j];
}
data[j+1] = tmp;
}
}
}
大概就是这样,有什么问题问我吧。
希望能对你有所帮助,谢谢
㈦ java数组的插入排序
什么狗蛋书,完全不知道在搞什么.
要排序直接用API里的Arrays工具类就好.要学原理那都是数学的问题.代码讲究不要重复发明轮子.
排序这种大众功能肯定已经有高手写好了效率非常高的.完全没有必要再去写
㈧ java怎么实现排序
Java实现几种常见排序方法
日常操作中常见的排序方法有:冒泡排序、快速排序、选择排序、插入排序、希尔排序,甚至还有基数排序、鸡尾酒排序、桶排序、鸽巢排序、归并排序等。
以下常见算法的定义
1. 插入排序:插入排序基本操作就是将一个数据插入到已经排好序的有序数据中,从而得到一个新的、个数加一的有序数据,算法适用于少量数据的排序,时间复杂度为O(n^2)。是稳定的排序方法。插入排序的基本思想是:每步将一个待排序的纪录,按其关键码值的大小插入前面已经排序的文件中适当位置上,直到全部插入完为止。
2. 选择排序:选择排序(Selection sort)是一种简单直观的排序算法。它的工作原理是每一次从待排序的数据元素中选出最小(或最大)的一个元素,存放在序列的起始位置,直到全部待排序的数据元素排完。 选择排序是不稳定的排序方法。
3. 冒泡排序:冒泡排序(Bubble Sort),是一种计算机科学领域的较简单的排序算法。它重复地走访过要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来。走访数列的工作是重复地进行直到没有再需要交换,也就是说该数列已经排序完成。这个算法的名字由来是因为越大的元素会经由交换慢慢“浮”到数列的顶端。
4. 快速排序:快速排序(Quicksort)是对冒泡排序的一种改进。它的基本思想是:通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据都要小,然后再按此方法对这两部分数据分别进行快速排序,整个排序过程可以递归进行,以此达到整个数据变成有序序列。
5. 归并排序:归并排序是建立在归并操作上的一种有效的排序算法,该算法是采用分治法(Divide and Conquer)的一个非常典型的应用。将已有序的子序列合并,得到完全有序的序列;即先使每个子序列有序,再使子序列段间有序。若将两个有序表合并成一个有序表,称为二路归并。
6. 希尔排序:希尔排序(Shell Sort)是插入排序的一种。也称缩小增量排序,是直接插入排序算法的一种更高效的改进版本。希尔排序是非稳定排序算法。希尔排序是把记录按下标的一定增量分组,对每组使用直接插入排序算法排序;随着增量逐渐减少,每组包含的关键词越来越多,当增量减至1时,整个文件恰被分成一组,算法便终止。
https://www.cnblogs.com/wangmingshun/p/5635292.html
㈨ 关于java直接插入算法的问题
/**这是一个利用直接插入排序法写的一个小程序;
直接插入排序是一个将待排序列中的元素p[i]与一个有序序列中的元素q[j--]比较(从后向前),当p[i] >= q[j] (递增排序)或 p[i] <= q[j] (递减排序)时,q[j+1] = p[i];反之就将q[j]移位到q[j+1]为p[i]的插入预留空间且如果j==0则q[j] = p[i].
*/
public class SISort
{
public static int[] sortAscending(int []with){ //整数递增排序
int length = with.length; //获取待排数组的元素个数;
int []temp = new int[length];
temp[0] = with[0]; //定义一个只有一个元素的有序数组
for(int i=1; i<length; i++){
for(int j=i-1; j>=0;j--){
if(with[i] >= temp[j]){ //如果待排序列中的元素大于等于有有序序列中的元素,则插入
temp[j+1] = with[i];
break;
}
else {
temp[j+1] = temp[j]; //给待插入元素预留空间
if(j == 0)
temp[j] = with[i]; //with[[i]是有序序列中最小的,因此排在开头
}
}
}
return temp;
}
public static double[] sortAscending(double []with){ //带小数的递增排序
int length = with.length; //获取待排数组的元素个数;
double []temp = new double[length];
temp[0] = with[0]; //定义一个只有一个元素的有序数组
for(int i=1; i<length; i++){
for(int j=i-1; j>=0;j--){
if(with[i] >= temp[j]){ //如果待排序列中的元素大于等于有有序序列中的元素,则插入
temp[j+1] = with[i];
break;
}
else {
temp[j+1] = temp[j]; //给待插入元素预留空间
if(j == 0)
temp[j] = with[i]; //with[[i]是有序序列中最小的,因此排在开头
}
}
}
return temp;
}
public static double[] sortDescending(double []with){ //递减排序
int length = with.length; //获取待排数组的元素个数;
double []temp = new double[length];
temp[0] = with[0]; //定义一个只有一个元素的有序数组
for(int i=1; i<length; i++){
for(int j=i-1; j>=0;j--){
if(with[i] <= temp[j]){ //如果待排序列中的元素小于等于有有序序列中的元素,则插入
temp[j+1] = with[i];
break;
}
else {
temp[j+1] = temp[j]; //给待插入元素预留空间
if(j == 0)
temp[j] = with[i]; //with[[i]是有序序列中最大的,因此排在开头
}
}
}
return temp;
}
public static int[] sortDescending(int []with){ //递减排序
int length = with.length; //获取待排数组的元素个数;
int []temp = new int[length];
temp[0] = with[0]; //定义一个只有一个元素的有序数组
for(int i=1; i<length; i++){
for(int j=i-1; j>=0;j--){
if(with[i] <= temp[j]){ //如果待排序列中的元素小于等于有有序序列中的元素,则插入
temp[j+1] = with[i];
break;
}
else {
temp[j+1] = temp[j]; //给待插入元素预留空间
if(j == 0)
temp[j] = with[i]; //with[[i]是有序序列中最大的,因此排在开头
}
}
}
return temp;
}
/* public static void main(String[] args)
{
int []test1 = {2,6,5,8,7,9,10,256,248,14}; //测试数组
double []test2 = {1.1,2.0,3,5,6,8.9,99,5};
int []temp1; //中间变量
double []temp2;
temp1 = sortDescending(test1); //测试整数递减排序
System.out.println("get a Decreasing sequence ");
for(int i=0; i<temp1.length; i++){
System.out.println(temp1[i]);
}
temp1 = sortAscending(test1); //测试整数递增排序
System.out.println("get a Increasing sequence");
for(int i=0; i<temp1.length; i++){
System.out.println(temp1[i]);
}
temp2 = sortDescending(test2); //测试带小数递减排序
System.out.println("get a Decreasing sequence ");
for(int i=0; i<temp2.length; i++){
System.out.println(temp2[i]);
}
temp2 = sortAscending(test2); //测试带小数递增排序
System.out.println("get a Increasing sequence");
for(int i=0; i<temp2.length; i++){
System.out.println(temp2[i]);