Java SE 037 Exchange sort quick sort

(1) As long as a person does not give up on himself, the whole world will not give up on you.
(2) I am born to be of great use . (3) If I
cannot bear the suffering of learning, I must bear the suffering of life. How painful it is Deep comprehension.
(4) You must gain from doing difficult things . (
5) Spirit is the real blade.
(6) Conquering opponents twice, the first time in the heart.
(7) Writing is really not easy. If you like it or have something for you Help remember to like + follow or favorite~

Java SE 037 Exchange sort quick sort

1. Exchange sort

(1) Exchange sort is a big category. In the data structure, bubble sort is also a kind of exchange, the first and the second, the second and the third.
(2) Exchange refers to the first exchange with the second, the first exchange with the third, the first exchange with the fourth, so that after the exchange, the smallest or largest element comes out. After coming out, compare the second one with the third one, and compare the second one with the fourth one, and compare and exchange positions in turn.

2. Quick sort

Quick sort is also a kind of exchange sort, because it also involves the exchange of a large number of elements.
1. First find the element with the lowest index and the element with the largest index.
2. After sorting once, the value I find is smaller than this value and put to the left of this value, and the value larger than this value is put to the right of this value. In other words, I can find a value from here. . The first time is to place the value smaller than the found value on the left, and the value greater than the found value on the right.

Insert picture description here
Insert picture description here
Insert picture description here

3. Some ways about the range value in JDK

There are many methods in the JDK that can return a certain value in a range. Many methods are like this, and will often be encountered later. It has a uniform rule, that is, the returned value, if it is a range, it must be greater than or equal to the smaller one, and smaller than the larger one. It is such a law. Do not include the smaller one. The same is true for the nextInt() method in Random, which is greater than or equal to 0, and less than the larger one.

3. 1 Random number generation method 1

import java.util.Random;

public class RandomTest2{
    
    
	public static void main(String[] args){
    
    
		Random random = new Random();

		for(int i = 0 ; i < 50 ; i ++){
    
    
			//random.nextInt()方法,不管传入的数是什么,它产生的数字一定是从0开始的,包含0,不包含传入的数。我想产生一个介于[10,50]之间的数,10是0+10就可以了,所以
			//在这个地方可以产生一个0-40之间的随机数。0-40之间再加上一个10就变成了10-50之间了。如果产生0-40之间的随机数,则需要在nextInt()参数中传递一个41进去,即
			//nextInt(41); 10是一个确定的值,加上一个随机的值,还是一个随机的值。
			System.out.println(random.nextInt(41)+10);
		}
	}
}

java.lang.Math
0.0 <= n <1.0
Generate random numbers between [10,50]

0.0 <= n <41.0
converted to integer
0 <= n <41
plus 10 on both sides 10
10 <= n <51
10 <= n <=50

3.2 Random number generation method 2

import java.util.Random;

public class RandomTest2
{
    
    
	public static void main(String[] args){
    
    
		Random random = new Random();

		for(int i = 0 ; i < 50 ; i ++){
    
    
			//random.nextInt()方法,不管传入的数是什么,它产生的数字一定是从0开始的,包含0,不包含传入的数。我想产生一个介于[10,50]之间的数,10是0+10就可以了,所以
			//在这个地方可以产生一个0-40之间的随机数。0-40之间再加上一个10就变成了10-50之间了。如果产生0-40之间的随机数,则需要在nextInt()参数中传递一个41进去,即
			//nextInt(41); 10是一个确定的值,加上一个随机的值,还是一个随机的值。
			System.out.println(random.nextInt(41)+10);
		}
	}
}

3.3 Homework

(1) Randomly generate 50 numbers (integer), the range of each number is [10,50], count the number of occurrences of each number and the number with the most occurrences and its number, and finally combine each number and its number The number of occurrences is printed out. If the number of occurrences of a number is 0, do not print it. When printing, they are arranged in ascending order of numbers.

import java.util.Random;
/**
随机生成50个数字(整数),每个数字的范围是[10,50],统计每个数字出现的次数以
及出现次数最多的数字与它的个数,最后将每个数字及其出现次数打印出来,如果某
个数字出现次数为0,则不要打印它。打印时按照数字的升序排列。

数字界于10-50之间,因此最多可出现41种数字,但是一共要生成50个数字,产生的种
类是41种,也就是说产生的数字当中肯定会有重复的,50个数字里面要有41种,至少有
几个数字是重复出现的。
int[] count = new int[41];
这个就是统计的是产生的每一种数字,因为要统计它出现的次数,所以存放的是每一种数据。
*/
public class RandomTest4
{
    
    
	public static void main(String[] args){
    
    
		int[] count = new int[41];

		Random random = new Random();

		for(int i = 0 ; i < 50; i++){
    
    
		  int number = random.nextInt(41)+10;//产生一个基于[10,50]之间的随机数。
		  System.out.println(number);
		  //与它关联的,这个数字到底要出现多少次,一个是作为数组索引的信息,另外
		  //是作为数组索引处那个值的信息给保存起来。
		  count[number-10]++;//如果一个数字出现了一次,则将数组当中的第一个元素它
		  //的个数加1 number-10,为什么要减10,因为它正好是从10开始的。而数组索引
		  //正好是从0开始的,换句话说count[0]这个元素,存放的是数字10的出现次数,
		  //count[1]这个元素,存放的是数字11出现的次数,依次类推,count[40]这个元
		  //素存放的是50这个数字出现的次数。
		  //count 是一个整型数组,不需要给它指定初始值,生成的时候,每一个元素的值
		  //都为0,直接++就可以了。++就表示在原来的基础上加1,所以现在就通过一个数组
		  //的索引以及数组索引对应的值的信息把需要的两个信息全都给保存起来了。不需要
		  //定义第二个数组。			 
		}

		 //统计每个数字出现的次数
		  for(int i = 0 ; i < count.length ; i ++){
    
    
			//注意:每一个数字是数组当中元素索引对应的value.
			//每一个数字可以从索引处得到。
			
			if(0 == count[i]){
    
    
				continue;
			}
			System.out.println((10+i)+"出现次数:  "+count[i]);
		  }

		  //找到出现次数最多的数字
		  //首先要找到哪个数字出现次数最大,可以用Arrays里面的max方法,找到最大的数字,
		  //也可以通过先排序,排序之后找到索引最大的元素,排完序之后找到索引最大的元素,
		  //或者也可以一个个的去比较,按照线性的方式一个个去比较,如何比呢?
		  //开始认为数组当中第一个元素的值是最大的,把这个元素存放到一个变量里面,然后
		  //拿这个变量跟第二个元素比,如果第二个元素大于这个变量,就将第二个元素赋给这
		  //个变量。然后跟第三个比,一直比到最后一个。这个变量值就是最大的。
		  int max = count[0];
		  for(int i = 0 ; i < count.length; i++){
    
    
			if(max < count[i]){
    
    
				max = count[i];
			}
		  }

		  System.out.println("出现的最大次数为:"+max +"次");

		  //打印出这个数字是谁
		  for(int i = 0; i < count.length; i++){
    
    
			if(max == count[i]){
    
    
				System.out.println(i+10);
			}
		  }
	}
}

Guess you like

Origin blog.csdn.net/xiogjie_67/article/details/108501200