常见排序——Java实现
1 package struct;
2
3 /**
4 *
5 * @作者:dyy
6 * @公司:陕西科技大学
7 * @修改日期:
8 * @邮箱:1101632375@qq.com
9 * @描述:Java实现几种常见排序
10 */
11
12
13 //选择排序类
14 class SelectSort{
15 public void selectSort(int[] arr){
16 for(int i = 0;i < arr.length; i++){
17 int currentMax = i;//记录当前的最大值下标
18 for(int j = i + 1;j < arr.length;j++){
19 //找到最大值下标
20 if(arr[j] > arr[currentMax]){
21 currentMax = j;
22 }
23 }
24 int temp = arr[i];
25 arr[i] = arr[currentMax];
26 arr[currentMax] = temp;
27 }
28 }
29 public void print(int[] arr){
30 for(int i = 0;i<arr.length;i++){
31 System.out.print(arr[i]+" ");
32 }
33 }
34 }
35
36
37 //冒泡排序类
38 class BubbleSort{
39 //冒泡排序的实现
40 public void bubbleSort(int[] arr){
41 for(int i = 0;i < arr.length - 1;i++){
42 for(int j = 0;j < arr.length - 1 - i;j++){
43 if(arr[j] < arr[j+1]){
44 int temp = arr[j];
45 arr[j] = arr[j+1];
46 arr[j+1] = temp;
47 }
48 }
49 }
50 }
51 //打印数组
52 public void print(int[] a){
53 for(int i =0 ;i<a.length;i++){
54 System.out.print(a[i]+" ");
55 }
56 }
57 }
58
59
60 //插入排序
61 class InsertSort{
62 public void insertSort(int[] arr){
63 for(int i = 0; i < arr.length - 1;i++){
64 //将第一个元素当作排好序的
65 int j;
66 int insert = arr[i];
67 for(j = i;j > 0 && insert>arr[j-1];j--){
68 arr[j] = arr[j-1];
69 }
70 arr[j] = insert;
71 }
72 }
73
74 //打印数组
75 public void print(int[] a){
76 for(int i =0 ;i<a.length;i++){
77 System.out.print(a[i]+" ");
78 }
79 }
80 }
1 public class TestVeriousSort {
2 public static void main(String[] args) {
3 int[] arr1 = {13,2,6,34,1,4,9,7,5};
4 //选择排序
5 System.out.println("选择排序"+"\n");
6 SelectSort obj = new SelectSort();
7 System.out.println("初始的数组:");
8 obj.print(arr1);
9 System.out.println("\n"+"排序后的数组:");
10 obj.selectSort(arr1);
11 obj.print(arr1);
12
13 //冒泡排序
14 System.out.println("\n"+"冒泡排序"+"\n");
15 BubbleSort obj1 = new BubbleSort();
16 System.out.println("初始的数组:");
17 obj1.print(arr1);
18 System.out.println("\n"+"排序后的数组:");
19 obj1.bubbleSort(arr1);
20 obj1.print(arr1);
21
22 //插入排序
23 System.out.println("\n"+"冒泡排序"+"\n");
24 InsertSort obj2 = new InsertSort();
25 System.out.println("初始的数组:");
26 obj2.print(arr1);
27 System.out.println("\n"+"排序后的数组:");
28 obj2.insertSort(arr1);
29 obj2.print(arr1);
30 }
31 }
常见排序——Java实现的更多相关文章
- 常见排序java实现
public class Sort { public static void main(String[] args) { int[] data = {49,38,65,97,76,13,27,49}; ...
- 常见排序算法(附java代码)
常见排序算法与java实现 一.选择排序(SelectSort) 基本原理:对于给定的一组记录,经过第一轮比较后得到最小的记录,然后将该记录与第一个记录的位置进行交换:接着对不包括第一个记录以外的其他 ...
- 常见排序算法总结 -- java实现
常见排序算法总结 -- java实现 排序算法可以分为两大类: 非线性时间比较类排序:通过比较来决定元素间的相对次序,由于其时间复杂度不能突破O(nlogn),因此称为非线性时间比较类排序. 线性时间 ...
- Java基础-数组常见排序方式
Java基础-数组常见排序方式 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 数据的排序一般都是生序排序,即元素从小到大排列.常见的有两种排序方式:选择排序和冒泡排序.选择排序的特 ...
- 常见排序算法题(java版)
常见排序算法题(java版) //插入排序: package org.rut.util.algorithm.support; import org.rut.util.algorithm.Sor ...
- Java基础语法(8)-数组中的常见排序算法
title: Java基础语法(8)-数组中的常见排序算法 blog: CSDN data: Java学习路线及视频 1.基本概念 排序: 是计算机程序设计中的一项重要操作,其功能是指一个数据元素集合 ...
- 常见排序算法(java实现)
常见排序算法介绍 冒泡排序 代码: public class BubbleSort { public static void sort(int[] array) { int tValue; for ( ...
- 一些常见的Java面试题 & 面试感悟
< 前言 > 近期在面试,深感这个行业的浮躁,一些菜不辣基的弱鸡开出的工资待遇要求,超过了我.不知道他们是怎么拿到那么高的工资的,难道是他在公司有亲戚朋友吗?有后台吗?是行业热钱真的过多了 ...
- 最常见的Java面试题及答案汇总(三)
上一篇:最常见的Java面试题及答案汇总(二) 多线程 35. 并行和并发有什么区别? 并行是指两个或者多个事件在同一时刻发生:而并发是指两个或多个事件在同一时间间隔发生. 并行是在不同实体上的多个事 ...
随机推荐
- (三)lamp环境搭建之编译安装php
1,PRC (People's republic of China) timezone中设置的时间为中国时间. 2,php的官方镜像源,使用linux时可以直接下载的 http://cn2.php.n ...
- CentOS部署多台服务器JDK(shell脚本部署)
部署7台新服务器的jdk,数量不算多,但也不打算一台一台的部署,写了个脚本执行 [ #!/bin/bash# JDK 安装包名jdk_packge="jdk-8u162-linux-x64. ...
- Vue 基础自查——watch、computed和methods的区别
1 前言 创建一个Vue实例时,可以传入一个选项对象 const vm = new Vue({ data: { msg: 'hello' }, computed: {}, methods: {}, w ...
- 体验.NET Core使用IKVM对接Java
前言 与第三方对接最麻烦的是语言不同,因语言不同内置实现相关标准加密算法还是略微有所差异,对接单点登录场景再寻常不过,由于时间紧迫且对接方使用Java,所以留给我对接开发和联调的时间本就不多,于是乎, ...
- Python--基本数据类型(可变/不可变类型)
目录 Python--基本数据类型 1.整型 int 2.浮点型 float 3.字符串 str 字符串格式 字符串嵌套 4.列表 list 列表元素的下标位置 索引和切片:字符串,列表常用 5.字典 ...
- [cf10E]Greedy Change
对于$w$的表示方案,可以用序列描述,即$x_{i}$表示第$i$种货币的数量 贪心策略得到的方案即是(对应序列)字典序最大的方案,并定义最优策略得到的方案为在最小化货币总数的基础上,(对应序列)字典 ...
- [atARC084D]Small Multiple
构造一张图:$\forall x$,向$10x$连一条边权为0的边,向$x+1$连1条边权为1的边,那么0到$i$的代价即为$i$各位数字之和 考虑到我们只关心于当前点的两个特征:1.模$n$的余数( ...
- 【Tool】MySQL卸载
MySQL卸载 2019-11-07 13:23:00 by冲冲 1.停止MySQL服务 右击"计算机" -- "管理" -- 左击"服务和应用程 ...
- Error occurred during initialization of VM Could not reserve enough space fo
通过es的elasticsearch.bat 启动.发现错误:Error occurred during initialization of VM Could not reserve enough s ...
- File与IO基础
IO流的作用:持久化到磁盘 File类的使用 File类基本概念 文件和文件夹都是用File类来表示. File类是内存层面的对象,内存中创建出来的File对象不一定有一个真实存在的文件或文件夹,但是 ...