【Java】Java复习笔记-三大排序算法,堆栈队列,生成无重复的随机数列

冒泡排序

 1 package com.lcw.bubble;
 2 
 3 public class BubbleSort
 4 {    
 5     /**
 6      * 冒泡排序
 7      * @param args
 8      * @author 成鹏致远
 9      */
10     
11     public static int[] bubleSort(int[] before)
12     {
13         int temp;
14         for (int i = 0; i < before.length; i++)
15         {
16             for (int j = 0; j < before.length-i-1; j++)//依次进行排序
17             {
18                 if (before[j] > before[j+1])
19                 {
20                     temp = before[j+1];
21                     before[j+1] = before[j];
22                     before[j] = temp;
23                 }
24             }
25         }
26         return before;
27     }
28     
29 }
View Code

选择排序 

 1 package com.lcw.select;
 2 /**
 3  *@author 成鹏致远
 4  *@net http://infodown.tap.cn 
 5  */
 6 public class SelectionSort
 7 {
 8     public static  void selectionSort(int[] number)
 9     {
10         for(int i=0; i<number.length-1; i++)
11         {
12             int m =i;//每次确定一个最小数
13             for (int j=i+1; j<number.length; j++)
14             {
15                 if(number[j] <number[m])
16                 {
17                     m =j;
18                 }
19             }
20             if (i != m)
21             {
22                 swap(number,i,m);
23             }
24         }
25     }
26     
27     private static void swap(int[] number, int i, int j)//用于交换数组中的索引为i,j的数
28     {
29         int t;
30         t = number[i];
31         number[i] = number[j];
32         number[j] = t;
33     }
34 }
View Code

快速排序

 1 package com.lcw.quick;
 2 
 3 /**
 4  * @author 成鹏致远
 5  * @net http://infodown.tap.cn
 6  */
 7 public class Quick
 8 {
 9     //排序方法,接受一个int[]参数,将会调用快速排序方法进行排序
10     public static void sort(int[] number)
11     {
12         quickSort(number, 0, number.length-1);
13     }
14     
15     public static void quickSort(int[] number, int left, int right)
16     {
17         if (left < right)
18         {
19             int s = number[left];//基准元素
20             int i = left;
21             int j = right+1;
22             while (true)
23             {
24                 //向右找大于s的数的索引
25                 while(i+1 < number.length && number[++i] < s);
26                 //向左找小于s的数的索引
27                 while(j-1 > -1 && number[--j] > s);
28                 //如果i>=j;退出循环
29                 if (i >= j)
30                 {
31                     break;
32                 }
33                 //否则交换索引i 和j 的元素
34                 swap(number,i,j);
35             }
36             //此时 number[left]为基准元素
37             //      number[(left+1)~i]<number[left]
38             //      number[(j+1)~right]>number[left]
39             //      number[j]存放最小数(j从右往左,比较完所有的数)            
40             number[left] = number[j];//最小数放在数组的最前面
41             number[j] = s;//s为基准元素,遍历一遍后,从j位置分为两个无序数组继续递归排序
42             
43             //对左边进行递归
44             quickSort(number, left, j-1);
45             //对右边进行递归
46             quickSort(number, j+1, right);
47         }
48     }
49     
50     private static void swap(int[] number, int i, int j)//用于交换数组中的索引为i,j的数
51     {
52         int t;
53         t = number[i];
54         number[i] = number[j];
55         number[j] = t;
56     }
57 }
View Code

 三大排序算法测试代码

 1 package com.lcw.test;
 2 
 3 import java.util.Arrays;
 4 
 5 import com.lcw.bubble.BubbleSort;
 6 import com.lcw.quick.Quick;
 7 import com.lcw.select.SelectionSort;
 8 import com.sun.xml.internal.bind.v2.runtime.unmarshaller.XsiNilLoader.Array;
 9 
10 public class Test
11 {
12     public static void main(String[] args)
13     {
14         int test[] = {4,3,5,9,1};    //数组的静态初始化
15         
16 //        BubbleSort.bubleSort(test);    //冒泡排序
17 //        Arrays.sort(test);    //利用Arrays类的静态方法对数组进行排序
18 //        SelectionSort.selectionSort(test);//选择排序
19         Quick.sort(test);//快速排序
20         
21         System.out.println("排序后:");
22         for (int i=0; i<test.length; i++)
23         {
24             System.out.println(test[i]);
25         }
26     }
27     
28 }
View Code

 队列

 1 package com.lcw.queue;
 2 
 3 public class Queue
 4 {
 5     /**
 6      * 队列
 7      * @param args
 8      * @author 成鹏致远
 9      * @net http://infodown.tap.cn
10      */
11 
12     private int front = -1, rear = -1;//队头和队尾
13     private String[] queue;//定义一个数组模拟队列
14     
15     public Queue(int maxElements)//构造器,参数为队列长度
16     {
17         queue = new String[maxElements];
18     }
19     
20     public void enqueue(String s)//入列
21     {
22         queue[++rear] = s;
23     }
24     
25     public boolean isEmpty()//判断是否为空
26     {
27         return front == rear;
28     }
29     
30     public boolean isFull()//判断是否已满
31     {
32         return rear== queue.length - 1;
33     }
34     
35     public String dequeue()//出列
36     {
37         return queue[++front];
38     }
39 }
View Code 

 队列测试代码

 1 package com.lcw.test;
 2 
 3 import com.lcw.queue.Queue;
 4 
 5 public class Test
 6 {
 7     public static void main(String[] args)
 8     {
 9         Queue queue = new Queue(10);
10         
11         queue.enqueue("1->lcw");
12         queue.enqueue("2->mystery");
13         
14         System.out.println("队列中第一个元素:"+queue.dequeue());
15         System.out.println("队列中第二个元素:"+queue.dequeue());
16         
17         if (queue.isEmpty())
18         {
19             System.out.println("队列已空!");
20         }
21     }
22     
23 }
View Code

 栈

 1 package com.lcw.stack;
 2 
 3 public class Stack
 4 {
 5     /**
 6      * 栈
 7      * @param args
 8      * @author 成鹏致远
 9      * @net http://infodown.tap.cn
10      */
11 
12     private int capacity = 100;
13     private String[] items;
14     private int top = 0;
15     
16     public Stack()//不带参构造器
17     {
18         this(100);
19     }
20 
21     public Stack(int cap)//带参数构造器
22     {
23         this.capacity = cap;
24         items = new String[cap];
25     }
26     
27     public void push(String s)//入栈
28     {
29         top++;
30         items[top] = s;
31     }
32     
33     public void pop()//出栈
34     {
35         items[top] = null;
36         top--;
37     }
38     
39     public void empty()//清空堆栈
40     {
41         top = 0;
42     }
43     
44     public String top()//取出最顶端的堆栈元素
45     {
46         return items[top];
47     }
48     
49     public int size()//获取堆栈元素个数
50     {
51         return top;
52     }
53 }
View Code

栈测试代码 

 1 package com.lcw.test;
 2 
 3 import com.lcw.stack.Stack;
 4 
 5 public class Test
 6 {
 7     public static void main(String[] argv)
 8     {
 9         Stack myStack = new Stack(5);
10         
11         myStack.push("lcw");
12         myStack.push("mystery");
13         
14         System.out.println("堆栈中元素个数:"+myStack.size());
15         System.out.println("最上面的是:"+myStack.top());
16         
17         myStack.pop();
18         System.out.println("POP后->堆栈中元素个数:"+myStack.size());
19         System.out.println("POP后->最上面的是:"+myStack.top());
20     }
21 }
View Code 

生成不重复的随机数队列 

 1     /**
 2      * 生成不重复的随机数
 3      * @param args
 4      * @author 成鹏致远
 5      * @net http://infodown.tap.cn
 6      */
 7 
 8         Random random = new Random(System.currentTimeMillis());
 9         //利用Random(当前时间)生成随机数
10         
11         StudentInfo info = new StudentInfo();    
12         
13         System.out.print("请输入需要输出的学生信息个数:");
14         Scanner sc = new Scanner(System.in);
15         int num = sc.nextInt();    //得到需要输出学生信息的个数
16         
17         int record[] = new int[num];
18         int intRd = 0;//存放随机数
19         int count = 0;//记录生成的随机数个数
20         boolean IsRecord = false;//是否已经生成过标志
21 //        System.out.println("数组的长度是:"+record.length);
22         
23         while (count < num)//生成指定范围内无重复的随机数
24         {
25             intRd = Math.abs(random.nextInt()%30);
26             //将随机数限制为30以下的非负数
27             for (int i = 0; i < count; i++)
28             {
29                 if(record[i] == intRd)
30                 {
31                     IsRecord = true;
32                     break;
33                 }
34                 else
35                 {
36                     IsRecord = false;
37                 }
38             }
39             if(!IsRecord)
40             {
41                 record[count++] = intRd;
42             }
43         }
44         
45         for(int a=0; a<num; a++)
46         {    
47             info.getStudentInfo(record[a]);
48         }
View Code
原文地址:https://www.cnblogs.com/lcw/p/3204651.html