java 数组冒泡排序、转置(降序)
1.java 数组冒泡排序
排序的基本原理(升序):
- 原始数据: 2 、1 、9 、0 、5 、3 、7 、6 、8;
- 第一次排序: 1 、2 、0 、5 、3 、7 、6 、8 、9 ;
- 第二次排序: 1 、0 、2 、3 、5 、6 、7 、8 、9 ;
- 第三次排序 : 1 、 2 、3 、4 、5 、6 、7 、8 、9 ;
以上是基础的原理过程,但是有一个问题,数据的不同可能排序的次数出现不同,但是有多少个数据,总的排序次数不会超过数组的长度。只要排序的次数达到长度*长度的次数,那么所有的数据就可以排序成功。
进行冒泡排序:
public class 数组的排序 {
public static void main(String[] args) {
int data[] = new int[]{2,1,9,0,5,3,7,6,8};
get(data);
//外层控制排序的总次数
for (int y = 0 ; y < data.length ; y++){
//内层控制每次的排序控制
for (int x = 0 ; x <data.length-1 ; x++) {
if (data[x] > data[x+1]){
int t = data[x];
data[x] = data[x+1];
data[x+1] = t;
}
}
}
get(data);
}
//输出数据的方法
public static void get(int temp[] ){
for (int i = 0 ; i < temp.length ; i++) {
System.out.print(temp[i]+ "、");
}
System.out.println();
}
}
改善设计:主方法设计上是作为程序的起点,既然是起点,所有的代码编写一定尽量简单,那么我们可以单独定义方法进行复杂操作。
public class 数组的排序 {
public static void main(String[] args) {
int data[] = new int[]{2,1,9,0,5,3,7,6,8};
get(data);
sort(data);
get(data);
}
//输出数据的方法
public static void get(int temp[] ){
for (int i = 0 ; i < temp.length ; i++) {
System.out.print(temp[i]+ "、");
}
System.out.println();
}
//负责排序的方法
public static void sort(int s[]){
//外层控制排序的总次数
for (int y = 0 ; y < s.length ; y++){
//内层控制每次的排序控制
for (int x = 0 ; x <s.length-1 ; x++) {
if (s[x] > s[x+1]){
int t = s[x];
s[x] = s[x+1];
s[x+1] = t;
}
}
}
}
}
2.数组转置
- 原始数据 : 1 、 2 、3 、4 、5 、6 、7 、8 ;
- 转置后数据 : 8 、 7 、6 、5 、4 、3 、2 、1 ;
要实现转置的操作有两个思路:
定义一个新的数组,而后将原始数组按照排序的方式插入到新的数组之中,随后改变原始数组的引用;
public class 数组的排序 {
public static void main(String[] args) {
int data[] = new int[]{1,2,3,4,5,6,7,8};
//首先定义一个新的数组,长度与原始数组一致
int temp[] = new int[data.length];
int foot = data.length -1 ;
//对于新的数组按照索引由小到大的顺序循环
for (int i = 0 ; i < temp.length ; i++) {
temp[i] = data[foot];
foot--;
}
data = temp; //data转向temp ,而data的原始数据就成为了垃圾
get(data);
}
//输出数据的方法
public static void get(int temp[] ){
for (int i = 0 ; i < temp.length ; i++) {
System.out.print(temp[i]+ "、");
}
System.out.println();
}
}
虽然上面的算法实现了转置的操作,但是代码里会产生垃圾 data的原始数据就是垃圾。
- 利用算法,再一个数组上直接完成转置操作
- 原始数据: 1 、 2 、3 、4 、5 、6 、7 、8 ; //转换次数:数组长度 ÷ 2 记住不管数组是技术还是偶数转置次数一样
- 第一次转置: 8 、 2 、3 、4 、5 、6 、7 、1 ;
- 第二次转置 : 8 、 7 、3 、4 、5 、6 、2 、1 ;
- 第三次转置: 8 、 7 、6 、4 、5 、3 、2 、1 ;
- 第四次转置 : 8 、 7 、6 、5 、4 、3 、2 、1 ;
转置:
public class 数组的转置 {
public static void main(String[] args) {
int data[] = new int[]{1,2,3,4,5,6,7,8,9};
reverse(data);
get(data);
}
//负责转置操作
public static void reverse(int arr[]){
int start = 0;
int end = arr.length -1;
for (int i = 0; i < arr.length/2 ; i++) {
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start ++;
end --;
}
}
//此方法负责输出
public static void get(int temp[]){
for(int i = 0 ; i < temp.length ; i++) {
System.out.print(temp [i]+"、");
}
System.out.println();
}
}
java 数组冒泡排序、转置(降序)的更多相关文章
- js学习篇--数组按升序降序排列
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 【Java】数组升序和降序
int[] x={1,6,4,8,6,9,12,32,76,34,23}; 升序: Arrays.sort(x); 降序: resort(x); public int[] resort(int[] n ...
- 【转】java comparator 升序、降序、倒序从源码角度理解
原文链接:https://blog.csdn.net/u013066244/article/details/78997869 环境jdk:1.7+ 前言之前我写过关于comparator的理解,但是都 ...
- Java中sort实现降序排序
利用Collections的reverseOrder方法: import java.util.Arrays; import java.util.Collections; public class Ma ...
- java数组冒泡排序
public class PaiXu1 { public static void main(String[] args) { int[] s = {345,879,456,870,221,902,14 ...
- Java里的数组降序
Java升序容易,降序不易. 基本类型不能降序,至少要是包装类. 升序使用Arrays.sort() 降序要么使用Collections.reverse,要么实现Comparator接口 import ...
- Java的数组和list升序,降序,逆序函数Collections.sort和Arrays.sort的使用
list升序,降序,逆序List<Integer>list =new ArrayList<Integer>();//如果list是 5 7 2 6 8 1 41.升序:Coll ...
- Comparator的compare方法如何定义升序降序
最近做算法题用了Comparator接口下的compare方法,思考了一下升序和降序的规则是如何来的,现在做一个补充,方便以后回顾. 升序代码 public static void main(Str ...
- java数组降序排序之冒泡排序
import java.util.Arrays;//必须加载 class Demo{ public static void main(String []args){ int[] arr={3,54,4 ...
随机推荐
- horizon源码分析(二)
源码版本:H版 一.简要回顾 对于请求: 地址:/dashboard/admin/instances/ 方式:POST 参数: instances_filter_q: action:instances ...
- 2017北京国庆刷题Day3 afternoon
期望得分:100+0+30=130 实际得分:100+36.5+0=136.5 T3 一个变量写混了,丢了30.. 模拟栈 #include<cstdio> #include<cst ...
- 莫队+分块 BZOJ 3809
3809: Gty的二逼妹子序列 Time Limit: 80 Sec Memory Limit: 28 MBSubmit: 1634 Solved: 482[Submit][Status][Di ...
- [Luogu 2023] AHOI2009 维护序列
[Luogu 2023] AHOI2009 维护序列 恕我冒昧这和线段树模板二有个琴梨区别? #include <cstdio> int n,m; long long p; class S ...
- TED_Topic2:My desperate journey with a human smuggler
My desperate journey with a human smuggler By Barat Ali Batoor When I was a child there was a toy wh ...
- 【CODEVS】1922 骑士共存问题
[算法]二分图最大匹配(最大流) [题解]按(i+j)奇偶性染色后,发现棋子跳到的地方刚好异色. 然后就是二分图了,对于每个奇点向可以跳到的地方连边,偶点不需连(可逆). 所以题目要求转换为求二分图上 ...
- Spring Boot工程结构推荐
工程结构(最佳实践) Spring Boot框架本身并没有对工程结构有特别的要求,但是按照最佳实践的工程结构可以帮助我们减少可能会遇见的坑,尤其是Spring包扫描机制的存在,如果您使用最佳实践的工程 ...
- c++树,知道前序和中序求后序遍历
经常有面试题就是知道一棵树的前序遍历和中序遍历让你写出后序遍历,这个慢慢画是能画出来的,但是要很快的弄出来还是要懂原理. 首先说一下三种遍历:所谓的前序后序和中序都是遍历时遍历根节点的顺序.子树的话依 ...
- 2017ACM暑期多校联合训练 - Team 6 1002 HDU 6097 Mindis (数学)
题目链接 Problem Description The center coordinate of the circle C is O, the coordinate of O is (0,0) , ...
- 一个Servlet处理增删改查的方法
处理的思路是在servlet中定义不同的增删改查方法,页面请求 的时候携带请求的参数,根据参数判断调用不同的方法. package cn.xm.small.Servlet; import java.i ...