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 ...
随机推荐
- [LeetCode] 25. Reverse Nodes in k-Group ☆☆☆
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. k ...
- Bayesian optimisation for smart hyperparameter search
Bayesian optimisation for smart hyperparameter search Fitting a single classifier does not take long ...
- JAVA中反射机制五(JavaBean的内省与BeanUtils库)
内省(Introspector) 是Java 语言对JavaBean类属性.事件的一种缺省处理方法. JavaBean是一种特殊的类,主要用于传递数据信息,这种类中的方法主要用于访问私有的字段,且方法 ...
- 数组A - 财务管理
Larry graduated this year and finally has a job. He's making a lot of money, but somehow never seems ...
- Django之原生Ajax操作
Ajax主要就是使用 [XmlHttpRequest]对象来完成请求的操作,该对象在主流浏览器中均存在(除早起的IE),Ajax首次出现IE5.5中存在(ActiveX控件). 先通过代码来看看Aja ...
- 用js拼接url为pathinfo模式
用js拼接url为pathinfo模式
- Hive ORC表的使用
创建普通临时表: create table if not exists test_orc_tmp( name string, gender string, cnt BIGINT )row ...
- MySQL join 用法
select column1, column2 from TABLE1 join TABLE2 on 条件 # select * from table1 join table2; #两个表合成一个se ...
- C/C++——[05] 函数
函数是 C/C++语言中的一种程序组件单位.一个函数通常代表了一种数据处理的功能,由函数体和函数原型两部分组成.函数原型为这个数据处理功能指定一个标识符号(函数的名称).说明被处理数据的组成及其类型. ...
- sklearn逻辑回归(Logistic Regression)类库总结
class sklearn.linear_model.LogisticRegression(penalty=’l2’, dual=False, tol=0.0001, C=1.0, fit_inter ...