java中二分法排序_java实现排序(二分法,选择排序,直接插入排序,冒泡排序,希尔排序)...

二分法

package com.ycit.sortSelect;

/**

* @author Administrator

* 二分法排序

*/

public class BinaryInsertSort {

public void sort(int a[]){

for(int i = 0;i

int left = 0; //left为有序数组的第一个下标

int right = i-1; //right为将要比较的数在数组的下标-1 ,即有序数组的最后一个下标

int mid = 0;

int temp = 0;

temp=a[i];// temp和有序数组做比较

while(left<=right){ //如果left小于right则一直寻找,直到left 大于 right

mid=(left+right)/2;

if(a[i]

right=mid-1; //若待比较的值比mid小 ,left不变 right变为mid-1

}else{

left=mid+1;//若待比较的值比mid大,right不变 left变为mid+1

}

}

for(int j =i-1;j>=left;j--){

a[j+1]=a[j]; // left 及left后面的数统一往后面挪一个下标

}

if(i!=left){

a[left]=temp;//将待比较值插入到到left中,

}

}

for(int array:a){

System.out.println(array);

}

}

public static void main(String[] args) {

BinaryInsertSort sort = new BinaryInsertSort();

sort.sort(new int[]{49,38,65,97,176,213,227,49,78,34,12,164,11,18,1});

}

}

选择排序

package com.ycit.sortSelect;

import java.sql.Array;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.util.Map;

/**

*

* @author Administrator

*选择排序

*/

public class SelectSort {

public void selectSort(int array[]){

int min;

int temp = 0;

for(int i = 0;i

min = array[i];

for(int j = i;j

if(min

min = array[j];

temp = array[i];

array[i] = min;

array[j] = temp;

}

}

}

for(int sort : array){

System.out.println(sort);

}

}

public static void main(String[] args) {

SelectSort s = new SelectSort();

s.selectSort(new int[]{8,3,4,5,7,1,12,33,15});

}

}

直接插入排序

package com.ycit.sortSelect;

/**

* @author Administrator

*2019年4月14日09:38:31

*直接插入排序

*时间内复杂度:n n-1 n-2 n-3 n-4 O(n^2/2)

*

*/

public class InsertSort {

public static void main(String args[]){

int [] array = new int[]{-1,9,6,4,2,10};

int j ;

for(int i = 1;i

int temp = array[i]; //temp和数组前面的有序数组进行比较

for(j=i-1;i>=0;j--){ //这边是从temp要比较的数字从后往前比较,数据前面都是有序的,如果要找到比temp大的数从后往前找的话效率会更高

if(array[j]>temp){

array[j+1]=array[j]; //找到比temp大的数统一往后移一位

}else{

break; //如果找到比temp小的数则退出循环

}

}

//找到比temp小的数的索引+1就是temp要插入的位置

array[j+1]=temp;

}

// 直接排序完成

for(int i =0;i

System.out.println("排序后"+array[i]);

}

}

}

希尔排序

package com.ycit.sortSelect;

/**

* @author Administrator 希尔排序 2019年4月14日13:51:40

*/

public class HeerSort {

public static void main(String[] args) {

int a[] = { 49, 38, 65, 97, 176, 213, 227, 49, 78, 34, 12, 164, 11, 18, 1 };

int d = a.length / 2; // 设定默认增量

while (true) {

for (int i = 0; i < d; i++) {// 根据增量依次递减提高排序效率

for (int j = i; j + d < a.length; j += d) { // 按增量对数据进行分组并排序

int temp;

if (a[j] > a[j + d]) {

temp = a[j];

a[j] = a[j + d];

a[j + d] = temp;

}

}

}

if (d == 1) {

break;

} // 如果增量等于1时退出比较

d--;

}

for (int afterSort : a) {

System.out.println(afterSort);

}

}

}

冒泡排序

package com.ycit.sortSelect;

/**

* @author Administrator 冒泡排序

*/

public class BubleSort {

public static void main(String[] args) {

int a[] = { 49, 38, 65, 97, 176, 213, 227, 49, 78, 34, 12, 164, 11, 18, 1 };

for (int i = 0; i < a.length; i++) {

for (int j = i + 1; j < a.length; j++) {

int temp;

if (a[i] > a[j]) {

temp = a[i];

a[i] = a[j];

a[j] = temp;

}

}

}

for (int array : a) {

System.out.println(array);

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
二分法排序冒泡排序是两种不同的排序算法。 二分法排序是一种基于比较的排序算法,其原理类似于数学的二分法。它的算法思想是,在总共有N个元素的数组,当插入第i个元素时,对前面的0~i-1个元素进行折半查找,先跟间的元素比较,如果小于间元素,则再对前一半的元素进行折半查找;否则,对后一半的元素进行折半查找。重复这个过程,直到左边的索引小于右边的索引,然后将第i个元素插入目标位置,并将目标位置之间的所有元素后移。最后得到一个有序的数组。 冒泡排序是一种简单但效率较低的排序算法。它的基本思想是通过相邻元素的比较和交换来实现排序。在Java冒泡排序通常使用双层循环来实现,外层循环控制排序轮数,总循环次数为要排序数组的长度减1。而内层循环主要用于对比相邻元素的大小,以确定是否需要交换位置。随着排序轮数的增加,对比和交换次数逐渐减少,直到得到一个有序的数组。 以下是一个使用Java语言实现二分法排序冒泡排序的示例代码: ``` // 二分法排序 public static void binarySort(int[] arr) { int len = arr.length; for (int i = 0; i < len; i++) { int target = arr[i]; int left = 0; int right = i - 1; int mid; while (left <= right) { mid = (left + right) / 2; if (target < arr[mid]) { right = mid - 1; } else { left = mid + 1; } } for (int j = i - 1; j >= left; j--) { arr[j + 1] = arr[j]; } arr[left] = target; } } // 冒泡排序 public static void bubbleSort(int[] arr) { int len = arr.length; for (int i = 0; i < len - 1; i++) { for (int j = 0; j < len - 1 - i; j++) { if (arr[j] > arr[j + 1]) { int temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值