当前位置:网站首页>Colon sorting code implementation
Colon sorting code implementation
2022-07-24 18:53:00 【Programming Altman】
package com.itheima_01;
/*
Firing sequence :
A way of sorting , Compare the adjacent data in the data to be sorted , Put larger data behind
Operate all data in turn , Until all data requirements are sorted
*/
public class ArrayDemo {
public static void main(String[] args) {
// Define an array
int[] arr = {
24, 69, 80, 57, 13};
System.out.println(" Before ordering :" + arrayToString(arr));
/* // The first comparison for (int i = 0; i < arr.length - 1 - 0; i++) { if (arr[i] > arr[i + 1]) { int temp = arr[i]; arr[i] = arr[i + 1]; arr[i + 1] = temp; } } System.out.println(" After the first comparison :" + arrayToString(arr)); // The second comparison for (int i = 0; i < arr.length - 1 - 1; i++) { if (arr[i] > arr[i + 1]) { int temp = arr[i]; arr[i] = arr[i + 1]; arr[i + 1] = temp; } } System.out.println(" After the second comparison :" + arrayToString(arr)); // The third comparison for (int i = 0; i < arr.length - 1 - 2; i++) { if (arr[i] > arr[i + 1]) { int temp = arr[i]; arr[i] = arr[i + 1]; arr[i + 1] = temp; } } System.out.println(" After the third comparison :" + arrayToString(arr)); // The fourth comparison for (int i = 0; i < arr.length - 1 - 3; i++) { if (arr[i] > arr[i + 1]) { int temp = arr[i]; arr[i] = arr[i + 1]; arr[i + 1] = temp; } } System.out.println(" After the fourth comparison :" + arrayToString(arr)); */
for (int x = 0 ; x <arr.length -1 ; x++) {
for (int i = 0; i < arr.length - 1 - x; i++) {
if (arr[i] > arr[i + 1]) {
int temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
}
}
}
System.out.println(" After ordering :" + arrayToString(arr));
}
// The elements in the array form a string according to the specified rules :[ Elements 1, Elements 2, ...]
public static String arrayToString(int[] arr) {
StringBuilder sb = new StringBuilder();
sb.append("[");
for (int i = 0; i < arr.length; i++) {
if (i == arr.length - 1) {
sb.append(arr[i]);
} else {
sb.append(arr[i]).append(",");
}
}
sb.append("]");
String s = sb.toString();
return s;
}
}
边栏推荐
- Principle and application of database
- Ionic4 learning notes 5-- custom public module
- Tcl/tk file operation
- DDR SDRAM board design guide
- ETL development tool kettle download installation environment construction and use tutorial
- 【TkInter】常用组件(一)
- Sequences, time series and prediction in tessorflow quizs on coursera (II)
- Calling startActivity() from outside of an Activity context requires the FLAG_ ACTIVITY_ NEW_ TASK flag
- OpenGL learning (IV) glut 3D image rendering
- 理解动态计算图,requires_grad、zero_grad
猜你喜欢

epoch,batch_ size

理解corners_align,两种看待像素的视角

Go Xiaobai implements a simple go mock server

MySQL1

Rookie colleagues cost me 2K. Did you recite the secret of salary increase? (collect it quickly!)

Calling startActivity() from outside of an Activity context requires the FLAG_ ACTIVITY_ NEW_ TASK flag

Ionic4 Learning Notes 6 -- using native ionic4 components in custom components

FPGA 20个例程篇:9.DDR3内存颗粒初始化写入并通过RS232读取(上)

暑期牛客多校1: I Chiitoitsu(期望dp,求逆元)

Ionic4 learning notes 5-- custom public module
随机推荐
About core files
投资的新阶段
Ionic4 learning notes 10 rotation map of an East Project
Excel practice notes 1
The assignment and answer of the "Cyberspace Security" competition of the 2020 secondary vocational group in Zhejiang Province (flag)
ETL development tool kettle download installation environment construction and use tutorial
PWN learning
为什么梯度是函数变化最快的方向
Ionic4 learning notes 13 - Classification List of an East Project
【TkInter】布局管理和事件系统
L4L7负载均衡
Tcl/tk file operation
OpenGL learning (IV) glut 3D image rendering
Data analysis of network security competition of national vocational college skills competition digital forensics-a
The difference between static method and instance method
Type-C PD protocol chip while charging and listening
Redis data type
Sqoop
Type-C边充边听PD协议芯片
Is the validity period of the root certificate as long as the server SSL certificate?