Bubble sorting with different input methods:
package sort;
import java.util.Scanner;
public class Sortbubbleone {
public static void main(String[] args) {
// TODO Auto-generated method stub
int i;
Scanner input=new Scanner(System.in);
System.out.println("请输入生成随机数的个数:");
int x=input.nextInt();
int b[]=new int[x];
for (int j = 0; j < b.length; j++) {
int temp=(int)(Math.random()*100);
b[j]=temp;
}
System.out.println("您输入的数字为:");
for ( i = 0; i < b.length; i++) {
System.out.print(b[i]+"\t");
}
System.out.println("\n排序后的数为:");
for (int j = 0; j < b.length; j++) {
for ( i = 0; i < b.length-j-1; i++) {
if(b[i]>b[i+1]) {
int temp=b[i+1];
b[i+1]=b[i];
b[i]=temp;
}
}
}
for (int j = 0; j < b.length; j++) {
System.out.print(b[j]+"\t");
}
}
}
package sort;
import java.util.Scanner;
public class Sortbubbletwo {
public static void main(String [] arge) {
Scanner input=new Scanner(System.in);
System.out.println("请输入数组的个数:");
int x=input.nextInt();
int[] bubble=new int[x];
System.out.println("请输入您要排序的数:");
for (int i = 0; i < bubble.length; i++) {
bubble[i]= input.nextInt();
}
System.out.println("您输入的数字为:");
for (int i = 0; i < bubble.length; i++) {
System.out.print(bubble[i]+"\t");
}
for (int i = 0; i <bubble.length ; i++) {
//第i冒泡,一次冒泡,会确定一个最大值
for (int j = 0; j <bubble.length-i-1 ; j++) {
//从头一直到已经确定的位置前,两两比较
int temp = bubble[j];
if(bubble[j]>bubble[j+1]){
bubble[j]=bubble[j+1];
bubble[j+1]=temp;
}
}
}
System.out.println("\n排序后的数字为:");
for (int i = 0; i < bubble.length; i++) {
System.out.print(bubble[i]+"\t");
}
}
}
Selection ordering with different input methods
package sort;
import java.util.Scanner;
public class Sortselectone {
public static void main(String[] args) {
// TODO Auto-generated method stub
int i;
Scanner input=new Scanner(System.in);
System.out.println("请输入生成随机数的个数:");
int x=input.nextInt();
int select[]=new int[x];
for (int j = 0; j < select.length; j++) {
int temp=(int)(Math.random()*100);
select[j]=temp;
}
System.out.println("您输入的数字为:");
for ( i = 0; i < select.length; i++) {
System.out.print(select[i]+"\t");
}
//选择排序开始
for( i = 0; i <select.length - 1 ; i++){
int min = i; // 遍历的区间最小的值
for (int j = i + 1; j <select.length ;j++){
if(select[j] < select[min]){
// 找到当前遍历区间最小的值的索引
min = j;
}
}
if(min != i){
// 发生了调换
int temp = select[min];
select[min] =select[i];
select[i] = temp;
}
}
System.out.println("\n排序后的数字为:");
for (i = 0; i < select.length; i++) {
System.out.print(select[i]+"\t");
}
}
}
package sort;
import java.util.Random;
import java.util.Scanner;
public class Sortselecttwo {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input=new Scanner(System.in);
int temp,i;
System.out.println("请输入数组的个数:");
int x=input.nextInt();
int[] select=new int[x];
System.out.println("请输入您要排序的数:");
for ( i = 0; i < select.length; i++) {
select[i]= input.nextInt();
}
System.out.println("您输入的数字为:");
for ( i = 0; i < select.length; i++) {
System.out.print(select[i]+"\t");
}
//选择排序开始
for( i = 0; i < select.length - 1 ; i++){
int min = i; // 遍历的区间最小的值
for (int j = i + 1; j < select.length ;j++){
if(select[j] < select[min]){
// 找到当前遍历区间最小的值的索引
min = j;
}
}
if(min != i){
// 发生了调换
temp = select[min];
select[min] =select[i];
select[i] = temp;
}
}
System.out.println("\n排序后的数字为:");
for (i = 0; i <select.length; i++) {
System.out.print(select[i]+"\t");
}
}
}
Quick sort with different input methods:
package sort;
import java.util.Scanner;
public class Sortfastone {
public static void main(String[] args){
Scanner input=new Scanner(System.in);
System.out.println("请输入生成随机数的个数:");
int x=input.nextInt();
int fast[]=new int[x];
for (int j = 0; j < fast.length; j++) {
int temp=(int)(Math.random()*100);
fast[j]=temp;
};
quickSort(fast, 0, fast.length-1);
for (int i = 0; i < fast.length; i++) {
System.out.println(fast[i]);
}
}
public static void quickSort(int[] fast,int low,int high){
int i,j,temp,t;
if(low>high){
return;
}
i=low;
j=high;
//temp就是基准位
temp = fast[low];
while (i<j) {
//先看右边,依次往左递减
while (temp<=fast[j]&&i<j) {
j--;
}
//再看左边,依次往右递增
while (temp>=fast[i]&&i<j) {
i++;
}
//如果满足条件则交换
if (i<j) {
t = fast[j];
fast[j] = fast[i];
fast[i] = t;
}
}
//最后将基准为与i和j相等位置的数字交换
fast[low] = fast[i];
fast[i] = temp;
//递归调用左半数组
quickSort(fast, low, j-1);
//递归调用右半数组
quickSort(fast, j+1, high);
}
}
package sort;
import java.util.Scanner;
public class Sortfasttwo {
public static void quickSort(int[] fast,int low,int high){
int i,j,temp,t;
if(low>high){
return;
}
i=low;
j=high;
//temp就是基准位
temp = fast[low];
while (i<j) {
//先看右边,依次往左递减
while (temp<=fast[j]&&i<j) {
j--;
}
//再看左边,依次往右递增
while (temp>=fast[i]&&i<j) {
i++;
}
//如果满足条件则交换
if (i<j) {
t = fast[j];
fast[j] = fast[i];
fast[i] = t;
}
}
//最后将基准为与i和j相等位置的数字交换
fast[low] = fast[i];
fast[i] = temp;
//递归调用左半数组
quickSort(fast, low, j-1);
//递归调用右半数组
quickSort(fast, j+1, high);
}
public static void main(String[] args){
/* int[] fast = {10,7,2,4,7,62,3,4,2,1,8,9,19}; */
Scanner input=new Scanner(System.in);
System.out.println("请输入数组中数的个数:");
int x=input.nextInt();
int[] fast=new int[x];
System.out.println("请输入要排序的数:");
for (int i = 0; i < fast.length; i++) {
fast[i]=input.nextInt();
}
System.out.println("您输入的数为:");
for (int i = 0; i < fast.length; i++) {
System.out.print(fast[i]+"\t");
}
quickSort(fast, 0, fast.length-1);
System.out.println("\n排序后的数为:");
for (int i = 0; i < fast.length; i++) {
System.out.print(fast[i]+"\t");
}
}
}