2021-07-21

数列排序

数列排序

问题描述给定一个长度为n的数列,将这个数列按从小到大的顺序排列。1<=n<=20000输入格式第一行为一个 整数n第二行包括n个整数,为待排序的数,每个整数的绝对值小于20000输出格式输出一行,按从小到大的顺序输出排序后的数列样例输入58 3 6 4 9 样例输出3 4 6 8 9 

方法一:

冒泡排序法

import java.util.Scanner;public class SequenceOfSorting { public static void main(String[] args) {  Scanner scanner = new Scanner(System.in);  int n = scanner.nextInt();  int []arr = new int[n];  //往数组里添加元素  for (int i = 0; i < n; i++) {   arr[i] = scanner.nextInt();  }  int temp = 0;  //排序  for (int i = 0; i < n; i++) {   for (int j = 0; j < n - i - 1; j++) {    if (arr[j] > arr[j + 1]){     temp = arr[j];     arr[j] = arr[j + 1];     arr[j + 1] = temp;    }   }  }  //打印数组  for (int i = 0; i < n; i++) {   System.out.print(arr[i] + " ");  } }}

运行结果
image

方法二:

直接插入排序

import java.util.Scanner;public class SequenceOfSorting2 { public static void main(String[] args) {  Scanner scanner = new Scanner(System.in);  int n = scanner.nextInt();  int []arr = new int[n];  for (int i = 0; i < n; i++) {   arr[i] = scanner.nextInt();  }  for (int i = 0; i < n; i++) {   for (int j = i;j > 0;j--){    if (arr[j - 1] > arr[j]){     int temp = arr[j - 1];     arr[j - 1] = arr[j];     arr[j] = temp;    }   }  }  for (int i = 0; i < n; i++) {   System.out.print(arr[i] + " ");  } }}

运行结果
image

方法三:

选择排序

import java.util.Scanner;public class SequenceOfSorting3 { public static void main(String[] args) {  Scanner scanner = new Scanner(System.in);  int n = scanner.nextInt();  int []arr = new int[n];  for (int i = 0; i < n; i++) {   arr[i] = scanner.nextInt();  }  for (int i = 0; i < n; i++) {   int temp = i;   for (int j = i;j < n;j++){    if (arr[j] < arr[temp]){ // 将数组里最小元素的下标赋值给temp     temp = j;......

原文转载:http://www.shaoqun.com/a/890891.html

跨境电商:https://www.ikjzd.com/

aca:https://www.ikjzd.com/w/1371

斑马物联:https://www.ikjzd.com/w/1316

国际标准书号:https://www.ikjzd.com/w/174


数列排序问题描述给定一个长度为n的数列,将这个数列按从小到大的顺序排列。1<=n<=20000输入格式第一行为一个整数n第二行包括n个整数,为待排序的数,每个整数的绝对值小于20000输出格式输出一行,按从小到大的顺序输出排序后的数列样例输入583649样例输出34689方法一:冒泡排序法importjava.util.Scanner;publicclassSequenceOfSort
太刺激了,张家界大峡谷蹦极开跳,你敢来体验吗?:http://www.30bags.com/a/225597.html
太干了!这些地方一个月滴雨未落!:http://www.30bags.com/a/221243.html
太罕见了!广东金子山惊现"神奇佛光",1小时出现了5次:http://www.30bags.com/a/223925.html
太好了!北京的隔壁有一条旅游景观大道,竟串起了太行最美山水!:http://www.30bags.com/a/221733.html
三个黑人玩一个少妇 两个男人一个前面一个后面:http://lady.shaoqun.com/a/247877.html
我接的黑人客人好痛苦 黑人锁住高潮也不拔出来:http://lady.shaoqun.com/a/247110.html
男人撕开奶罩揉吮奶头 我都说疼了他还在继续:http://lady.shaoqun.com/m/a/247840.html
趴在男朋友身上打光屁股 不吃饭被男朋友教训了:http://lady.shaoqun.com/m/a/248330.html
Lazada智能补货系统上线!:https://www.ikjzd.com/articles/146783
她们|不仅天生励志,还天生要强,她们是最美亚马逊跨境电商人:https://www.ikjzd.com/articles/146799
古代的寡妇和丫鬟是如何解决自己的需求的?:http://lady.shaoqun.com/a/427354.html
富二代开着法拉利去大学城接女生,不超过10分钟就轻松拿到校花:http://lady.shaoqun.com/a/427355.html

No comments:

Post a Comment