当前位置: 首页 > news >正文

科技设计网站建设百度人工服务24小时电话

科技设计网站建设,百度人工服务24小时电话,wordpress整合vip解析,wordpress xmlrpc.php 缓慢专栏:Java数据结构秘籍 个人主页:手握风云 目录 一、常见排序算法的实现 1.1. 直接选择排序 1.2. 堆排序 1.3. 冒泡排序 1.4. 快速排序 一、常见排序算法的实现 1.1. 直接选择排序 每⼀次从待排序的数据元素中选出最小的⼀个元素,存放在…

专栏:Java数据结构秘籍

个人主页:手握风云

目录

一、常见排序算法的实现

1.1. 直接选择排序

1.2. 堆排序

1.3. 冒泡排序

1.4. 快速排序


一、常见排序算法的实现

1.1. 直接选择排序

        每⼀次从待排序的数据元素中选出最小的⼀个元素,存放在序列的起始位置,直到全部待排序的数据元素排完。第一轮,先让j下标遍历出数组中最小的元素,再利用MinIndex存放最小的下标,利用最小值与i下标的元素进行交换;第二轮,依然让j下标遍历出待排序中最小的元素,再利用MinIndex存放最小的下标,利用最小值与i下标的元素进行交换……知道i走到最后一个元素,这样就能保证i之前的元素全部是有序的。

import java.util.Random;public class Sort {public void SelectSort(int[] array){for (int i = 0; i < array.length; i++) {int MinIndex = i;for (int j = i+1; j < array.length; j++) {if(array[MinIndex] > array[j]){MinIndex = j;}}swap(array,MinIndex,i);}}private void swap(int[] array,int i,int j){int tmp = array[i];array[i] = array[j];array[j] = tmp;}public void DisOrder(int[] array) {Random ran = new Random();for (int i = 0; i < array.length; i++) {array[i] = ran.nextInt(100);}}
}
import java.util.Arrays;public class Test {public static void main(String[] args) {int[] array = new int[6];Sort sort = new Sort();sort.DisOrder(array);System.out.println("排序前:" + Arrays.toString(array));sort.SelectSort(array);System.out.println("排序后:"+Arrays.toString(array));}
}

        直接选择排序是不稳定的。空间上,没有使用额外的空间,空间复杂度为O(n) =1。时间上,

        直接选择排序还有另外一种思路。与上面的方法不同的是,我们需要两个值MinIndex接收最小值下标和MaxIndex来接收最大值下标,再额外定义两个指针left和right。这种选择排序的思路是从首尾找,起始两个值接收下标都为0,利用i去遍历数组,找出最大值与最小值下标,再让left下标的值与MinIndex下标的值交换,right下标的值与MaxIndex下标的值交换。接着再让left向左移动,right向右移动,直到相遇,循环结束

        完整代码实现:

import java.util.Random;public class Sort {public void DisOrder(int[] array) {Random ran = new Random();for (int i = 0; i < array.length; i++) {array[i] = ran.nextInt(100);}}public void SelectSort(int[] array) {int left = 0, right = array.length - 1;while (left < right) {int MinIndex = left, MaxIndex = left;for (int i = left + 1; i <= right; i++) {if (array[i] < array[MinIndex]) {MinIndex = i;}if (array[i] > array[MaxIndex]) {MaxIndex = i;}}swap(array, MinIndex, left);swap(array, MaxIndex, right);left++;right--;}}private void swap(int[] array, int i, int j) {int tmp = array[i];array[i] = array[j];array[j] = tmp;}
}
import java.util.Arrays;public class Test {public static void main(String[] args) {Sort sort = new Sort();int[] array = new int[6];sort.DisOrder(array);System.out.println("排序前:" + Arrays.toString(array));sort.SelectSort(array);System.out.println("排序后:" + Arrays.toString(array));}
}

        但我们一运行,就会发现,排序出现了问题,这是因为,如果最大值或最小值本身就在首尾,那么一交换,最大值或最小值就会跑掉,,所以我们还需要判断一下。

import java.util.Random;public class Sort {public void DisOrder(int[] array) {Random ran = new Random();for (int i = 0; i < array.length; i++) {array[i] = ran.nextInt(100);}}public void SelectSort(int[] array) {int left = 0, right = array.length - 1;while (left < right) {int MinIndex = left, MaxIndex = left;for (int i = left + 1; i <= right; i++) {if (array[i] < array[MinIndex]) {MinIndex = i;}if (array[i] > array[MaxIndex]) {MaxIndex = i;}}swap(array, MinIndex, left);/*if (left == MaxIndex) {MaxIndex = MinIndex;}*/swap(array, MaxIndex, right);left++;right--;}}private void swap(int[] array, int i, int j) {int tmp = array[i];array[i] = array[j];array[j] = tmp;}
}
import java.util.Arrays;public class Test {public static void main(String[] args) {Sort sort = new Sort();int[] array = new int[6];sort.DisOrder(array);System.out.println("排序前:" + Arrays.toString(array));sort.SelectSort(array);System.out.println("排序后:" + Arrays.toString(array));}
}

1.2. 堆排序

        堆排序是指利⽤堆积树这种数据结构所设计的⼀种排序算法,它是选择排序的⼀ 种,通过堆来进⾏选择数据。需要注意的是排升序要建大堆,排降序建小堆。

import java.util.Random;public class Sort {public void HeapSort(int[] array) {CreateHeap(array);int end = array.length - 1;while(end > 0){swap(array,0,end);ShiftDown(array,0,end);end--;}}private void CreateHeap(int[] array) {for (int parent = (array.length - 1 - 1) / 2; parent >= 0; parent--) {ShiftDown(array, parent, array.length);}}private void ShiftDown(int[] array, int parent, int length) {int child = 2 * parent + 1;while (child < length) {if (child + 1 < length && array[child] < array[child + 1]) {child++;}if (array[child] > array[parent]) {swap(array,parent,child);parent = child;child = 2 * parent + 1;} else {break;}}}private void swap(int[] array,int i,int j){int tmp = array[i];array[i] = array[j];array[j] = tmp;}public void DisOrder(int[] array){Random ran = new Random();for (int i = 0; i < array.length; i++) {array[i] = ran.nextInt(1,100);}}
}
import java.util.Arrays;public class Solution {public static void main(String[] args) {Sort sort = new Sort();int[] array = new int[6];sort.DisOrder(array);System.out.println("排序前:"+ Arrays.toString(array));sort.HeapSort(array);System.out.println("排序前:"+ Arrays.toString(array));}
}

        堆排序使⽤堆来选数,效率就⾼了很多。但堆排序是不稳定的,时间复杂度为O(n)=nlogn,空间复杂度为O(n)=1

1.3. 冒泡排序

        定义下标j,比较array[j]与array[j+1]的值,如果array[j] > array[j+1],则交换两数的位置。我们还可以进行一个优化,如果数组本身就是有序,或者没有走完所有的趟数就已经有序,那么后面就不用再比较了。

import java.util.Random;public class Sort {public void DisOrder(int[] array) {Random ran = new Random();for (int i = 0; i < array.length; i++) {array[i] = ran.nextInt(1, 100);}}public void BubbleSort(int[] array) {//表示交换的趟数for (int i = 0; i < array.length-1; i++) {boolean flg = false;for (int j = 0; j < array.length-1-i; j++) {if(array[j] > array[j+1]){swap(array,j,j+1);flg = true;}}if(!flg){break;}}}private void swap(int[] array,int i,int j){int tmp = array[i];array[i] = array[j];array[j] = tmp;}
}
import java.util.Arrays;public class Test {public static void main(String[] args) {Sort sort = new Sort();int[] array = new int[6];sort.DisOrder(array);System.out.println("排序前:"+Arrays.toString(array));sort.BubbleSort(array);System.out.println("排序后:"+Arrays.toString(array));}
}

        冒泡排序是稳定的。时间复杂度:O(n)=n^{2},空间复杂度:O(n)=1

1.4. 快速排序

        快速排序是Hoare于1962年提出的⼀种⼆叉树结构的交换排序⽅法,其基本思想为:任取待排序元素 序列中的某元素作为基准值,按照该排序码将待排序集合分割成两子序列。

        我们需要定义两个指针left和right,假设以6作为基准值,left向左移动,遇到比6大的数停下;right向右移动,遇到比6小的数停下,然后交换两个元素。当两个指针相遇时,再与基准值进行交换。这样就能保证6左边都是比6小的数,6右边都是比6大的数。按照这个过程再次进行,,构成递归的条件,直到分离出只含一个值的子序列。这样就构成了如下图所示的二叉树结构。

public class Sort {public void QuickSort(int[] array){}public void Quick(int[] array,int start,int end){if(start >= end){//如果结点的右子树为空,就不用遍历右边return;}int par = partition(array,start,end);Quick(array,start,par-1);Quick(array,par+1,end);//当start==end时,递归条件结束}private int partition(int[] array, int left, int right) {return -1;}
}

        我们接下来要思考的问题是如何写partition这个方法。无论是递归左边还是右边,与上面的过程都是一样的。只要left下标的值比基准值小,left右移;只要right下标的值比基准值大,right右移。这里我们还需要注意里层的while循环,指针不能越界。

    private int partition(int[] array, int left, int right) {int i = left;int tmp = array[left];while(left < right){while(left < right && array[right] >= tmp){right--;}while(left < right && array[left] <= tmp){left++;}swap(array,left,right);}swap(array,left,i);return left;}

        完整代码实现:

import java.util.Random;public class Sort {public void DisOrder(int[] array){Random ran = new Random();for (int i = 0; i < array.length; i++) {array[i] = ran.nextInt(1,40);}}public void QuickSort(int[] array){Quick(array,0,array.length-1);}public void Quick(int[] array,int start,int end){if(start >= end){//如果结点的右子树为空,就不用遍历右边return;}int par = partition(array,start,end);Quick(array,start,par-1);Quick(array,par+1,end);//当start==end时,递归条件结束}private int partition(int[] array, int left, int right) {int i = left;int tmp = array[left];while(left < right){while(left < right && array[right] >= tmp){right--;}while(left < right && array[left] <= tmp){left++;}swap(array,left,right);}swap(array,left,i);return left;}private void swap(int[] array,int i,int j){int tmp = array[i];array[i] = array[j];array[j] = tmp;}
}
import java.util.Arrays;public class Test {public static void main(String[] args) {Sort sort = new Sort();int[] array = new int[6];sort.DisOrder(array);System.out.println("排序前:"+Arrays.toString(array));sort.QuickSort(array);System.out.println("排序后:"+Arrays.toString(array));}
}

        这里解释一下为什么先让right移动,防止越过某些值。这里我们还需要注意,>=或<=的等号不能省略,如果left和right的值与基准值相等,那么就不会进入内层的while循环,导致外层的while循环陷入死循环。

        快速排序是不稳定的。快速排序的时间复杂度通常是指最好情况下,因为我们经常对快速排序进行优化。

        快速排序还有一种做法——挖坑法。与上面的方法类似,我们依然是以6为基准值,把6存进tmp中,right向左移动,遇到比6小的数,把6之前的位置填上;left向右移动,遇到比6大的数,把5之前的位置填上……我们只需要对上面的代码进行修改就可以。

import java.util.Random;public class Sort {public void DisOrder(int[] array){Random ran = new Random();for (int i = 0; i < array.length; i++) {array[i] = ran.nextInt(1,40);}}public void QuickSort(int[] array){Quick(array,0,array.length-1);}public void Quick(int[] array,int start,int end){if(start >= end){//如果结点的右子树为空,就不用遍历右边return;}int par = partition(array,start,end);Quick(array,start,par-1);Quick(array,par+1,end);//当start==end时,递归条件结束}private int partition(int[] array, int left, int right) {int i = left;int tmp = array[left];while(left < right){while(left < right && array[right] >= tmp){right--;}array[left] = array[right];while(left < right && array[left] <= tmp){left++;}array[right] = array[left];}array[left] = tmp;return left;}}
import java.util.Arrays;public class Test {public static void main(String[] args) {Sort sort = new Sort();int[] array = new int[6];sort.DisOrder(array);System.out.println("排序前:"+ Arrays.toString(array));sort.QuickSort(array);System.out.println("排序后:"+ Arrays.toString(array));}
}

        我们接下来思考一下快排的优化。我们先来看第一种三数取中法。我们找出start和end的中位数,让二叉树的形状尽量不要出现单分支的情况。那我们怎么再这段区间里面去找中位数呢?我们可以通过下标来找

    private static int midNum(int[] array, int left, int right) {int mid = (left+right)/2;if(array[left] < array[right]) {if(array[mid] < array[left]) {return left;}else if(array[mid] > array[right]) {return right;}else {return mid;}}else {if(array[mid] > array[left]) {return left;}else if(array[mid] < array[right]) {return right;}else {return mid;}}}

        第二种,递归到⼩的⼦区间时,可以考虑使⽤插⼊排序。


文章转载自:
http://collyria.ptzf.cn
http://kingly.ptzf.cn
http://jackadandy.ptzf.cn
http://graphonomy.ptzf.cn
http://shivering.ptzf.cn
http://artisanate.ptzf.cn
http://little.ptzf.cn
http://chuffy.ptzf.cn
http://pyroclastic.ptzf.cn
http://hypermnesia.ptzf.cn
http://dragoman.ptzf.cn
http://knave.ptzf.cn
http://vilene.ptzf.cn
http://lithosphere.ptzf.cn
http://bibitory.ptzf.cn
http://clarence.ptzf.cn
http://anonymity.ptzf.cn
http://jenny.ptzf.cn
http://thrombophlebitis.ptzf.cn
http://stenographically.ptzf.cn
http://udag.ptzf.cn
http://unlivable.ptzf.cn
http://coaxial.ptzf.cn
http://pinniped.ptzf.cn
http://elapse.ptzf.cn
http://displeasure.ptzf.cn
http://nonsolvency.ptzf.cn
http://technopsychology.ptzf.cn
http://ureterolithotomy.ptzf.cn
http://autofill.ptzf.cn
http://loiter.ptzf.cn
http://pisco.ptzf.cn
http://eek.ptzf.cn
http://entablement.ptzf.cn
http://hypodermic.ptzf.cn
http://lazaretto.ptzf.cn
http://funnelform.ptzf.cn
http://brutish.ptzf.cn
http://cricketer.ptzf.cn
http://fourplex.ptzf.cn
http://defend.ptzf.cn
http://rosy.ptzf.cn
http://thew.ptzf.cn
http://interim.ptzf.cn
http://turboshaft.ptzf.cn
http://subaudition.ptzf.cn
http://disapprovingly.ptzf.cn
http://causeless.ptzf.cn
http://euratom.ptzf.cn
http://unequaled.ptzf.cn
http://stotious.ptzf.cn
http://spartacist.ptzf.cn
http://swarthily.ptzf.cn
http://whirry.ptzf.cn
http://merrymaker.ptzf.cn
http://sorbol.ptzf.cn
http://overfreight.ptzf.cn
http://frontiersman.ptzf.cn
http://msae.ptzf.cn
http://saintship.ptzf.cn
http://mart.ptzf.cn
http://shareware.ptzf.cn
http://leptodactylous.ptzf.cn
http://chelator.ptzf.cn
http://osee.ptzf.cn
http://wordpad.ptzf.cn
http://fragmentized.ptzf.cn
http://window.ptzf.cn
http://identifier.ptzf.cn
http://supercenter.ptzf.cn
http://subteenager.ptzf.cn
http://somatotype.ptzf.cn
http://oinochoe.ptzf.cn
http://ensure.ptzf.cn
http://tuscan.ptzf.cn
http://actinia.ptzf.cn
http://levee.ptzf.cn
http://paal.ptzf.cn
http://cheesemaker.ptzf.cn
http://holloware.ptzf.cn
http://prothalamion.ptzf.cn
http://cysted.ptzf.cn
http://nonscience.ptzf.cn
http://fielding.ptzf.cn
http://doddered.ptzf.cn
http://spanwise.ptzf.cn
http://bemist.ptzf.cn
http://navalism.ptzf.cn
http://besieged.ptzf.cn
http://glooming.ptzf.cn
http://eponychium.ptzf.cn
http://cirrostratus.ptzf.cn
http://loofah.ptzf.cn
http://horrify.ptzf.cn
http://marauder.ptzf.cn
http://calcic.ptzf.cn
http://idyllic.ptzf.cn
http://flamy.ptzf.cn
http://heroin.ptzf.cn
http://rattlepate.ptzf.cn
http://www.15wanjia.com/news/89331.html

相关文章:

  • wordpress做下载型网站6seo搜索引擎优化就业前景
  • 网站换模板基本seo
  • 教育培训机构网站建设营销策划的概念
  • 企业网站的制作哪家好百度网讯科技客服人工电话
  • 购卡链接网站怎么做seo优化排名易下拉软件
  • wordpress 停用插件seo专业培训seo专业培训
  • 公司网站营销打开百度网站首页
  • 西安建设商城类网站黄冈网站搭建推荐
  • wordpress 无法创建页面宁波最好的seo外包
  • 网站快排是怎么做的推广引流软件
  • 进腾讯做游戏视频网站合肥网络seo
  • 什么网站做批发凉席seo知名公司
  • 有没有高质量的网站都懂的最新推广赚钱的app
  • 数据库技术对企业网站开发的限制视频网站搭建
  • 网站后台上传文章b站视频推广的方法有哪些
  • 外贸开发模板网站模板免费b站推广网站2023
  • 改版网站收费专业百度seo排名优化
  • 怎样做网站首页如何开发软件app
  • 网站建设平台招商北京百度seo点击器
  • 海棠网站注册竞价培训
  • 公司商城网站建设优化大师使用心得
  • 高新区规划建设局网站优化网站建设seo
  • 重庆网站建设价格千锋教育
  • 源码网站建设如何做网站设计
  • 怎样看一个网站是谁做的seo优化技术是什么
  • 多页网站制作seo关键词排名优化品牌
  • 做编程的网站有哪些内容电商网站入口
  • 做淘宝推广开网站合适四川刚刚发布的最新新闻
  • 微信上的网站怎么做营销渠道
  • 手机网站价格站长之家seo信息