面试题一:使用 java 实现快速排序

来源:互联网 发布:linux 移动非空目录 编辑:程序博客网 时间:2024/06/05 12:46
快排:快排一般是以某个对象作为基础,这边以数字为例,比如有10个数字,我一般选择以第一个数字为基准
到后面查找,大于这个数字的,放到基准数右边,小于这个数字的,放到基准数左边。然后不断得执行下去,直到每个
数字都是一个单元


快排算法难度不大,看到这题目我一开始先写了一个int的快排,后面为了具有通用性
改成了对Object对象的一个排序,其中利用CompareTo函数进行对象之间的比较


Main.java 中写了三种测试方式,分别是int类型数据快排,char类型数据快排,

Object(Student)类型数据快排,在Student类中,我重写了CompareTo函数,改成通过stuId进行排序



package edu.fjnu.service;import java.util.Arrays;/** *  * @author Harry * 题目所需调用的业务逻辑方法 */public class SortMethod {private static int count = 1; //统计一共调整了几次/** * 快排核心排序实现 object(通用) * @param arr 对象数组 * @param begin 数组起始位置索引 * @param end 数组末端位置索引 */public void quickSortForObject(Object []arr,int begin,int end){if(begin < end) {int i = begin ;int j = end ;Object first = arr[begin]; while (i < j){while(i < j && ((Comparable<Object>)arr[j]).compareTo(first) >= 0) { j--;  System.out.println("第"+(count++)+"次调整结果: "+Arrays.toString(arr));//打印出每次调整的结果}            if(i < j) {            arr[i++] = arr[j];            System.out.println("第"+(count++)+"次调整结果: "+Arrays.toString(arr));//打印出每次调整的结果            }            while(i < j && ((Comparable<Object>)arr[i]).compareTo(first) < 0)   {i++; System.out.println("第"+(count++)+"次调整结果: "+Arrays.toString(arr));//打印出每次调整的结果}            if(i < j) {            arr[j--] = arr[i];            System.out.println("第"+(count++)+"次调整结果: "+Arrays.toString(arr));//打印出每次调整的结果            }                    }        arr[i] = first;        System.out.println("第"+(count++)+"次调整结果: "+Arrays.toString(arr));//打印出每次调整的结果quickSortForObject(arr,begin,i-1);quickSortForObject(arr,i+1,end);count = 1; //统计结束,重新设置为1}}/*public void quickSortForInt(int []arr,int begin,int end){if(begin < end) {int i = begin ;int j = end ;int first = arr[begin]; System.out.println("无调整的时候,数组是这样的"+Arrays.toString(arr));while (i < j){while(i < j && arr[j] >= first){j--;System.out.println("第"+(count++)+"次调整结果"+Arrays.toString(arr));//打印出每次调整的结果}            if(i < j){             arr[i++] = arr[j];            System.out.println("第"+(count++)+"次调整结果"+Arrays.toString(arr));//打印出每次调整的结果            }            while(i < j && arr[i] < first)   {i++;  System.out.println("第"+(count++)+"次调整结果"+Arrays.toString(arr));//打印出每次调整的结果}            if(i < j){             arr[j--] = arr[i];            System.out.println("第"+(count++)+"次调整结果"+Arrays.toString(arr));//打印出每次调整的结果            }                    }        arr[i] = first;        System.out.println("第"+(count++)+"次调整结果"+Arrays.toString(arr));//打印出每次调整的结果quickSortForInt(arr,begin,i-1);quickSortForInt(arr,i+1,end);}}public void quickSortForChar(char []arr,int begin,int end){if(begin < end) {int i = begin ;int j = end ;char first = arr[begin];         System.out.println("无调整的时候,数组是这样的"+Arrays.toString(arr));while (i < j){while(i < j && ((Comparable<Character>)arr[j]).compareTo(first) >= 0) {j--;  System.out.println("第"+(count++)+"次调整结果"+Arrays.toString(arr));//打印出每次调整的结果}            if(i < j) {            arr[i++] = arr[j];            System.out.println("第"+(count++)+"次调整结果"+Arrays.toString(arr));//打印出每次调整的结果            }            while(i < j && ((Comparable<Character>)arr[i]).compareTo(first) < 0)   {i++; System.out.println("第"+(count++)+"次调整结果"+Arrays.toString(arr));//打印出每次调整的结果}            if(i < j) {            arr[j--] = arr[i];            System.out.println("第"+(count++)+"次调整结果"+Arrays.toString(arr));//打印出每次调整的结果            }                    }        arr[i] = first;        System.out.println("第"+(count++)+"次调整结果"+Arrays.toString(arr));//打印出每次调整的结果quickSortForChar(arr,begin,i-1);quickSortForChar(arr,i+1,end);}}*/}


package edu.fjnu.domain;public class Student implements Comparable<Student> { //实现Comparable接口private Integer userId ;//用户idprivate String username ;//用户名private String password ;//用户密码public Student(int id,String name,String passwd){this.userId = id ;this.username = name ;this.password = passwd ; }public Integer getUserId() {return userId;}public void setUserId(Integer userId) {this.userId = userId;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}@Overridepublic String toString() {return "Id=" + userId + ", name=" + username+ ", passwd=" + password + "";}public int compareTo(Student o) { //重写compareTo函数,通过用户id来对对象进行排序if(this.userId > o.getUserId()){return 1 ;}if(this.userId < o.getUserId()){return -1 ;}return 0 ;//相等}}


package edu.fjnu.client;import java.util.Arrays;import org.junit.Test;import edu.fjnu.domain.Student;import edu.fjnu.service.SortMethod;/** *  * @author Harry * */public class Main {/** * @param args */public static void main(String[] args) {SortMethod forUse = new SortMethod();Object [] Int = {1,2,3,43,23,12,12};int intBegin = 0 ;int intEnd = Int.length -1 ;System.out.println("Int 数据初始结果为"+Arrays.toString(Int));forUse.quickSortForObject(Int, intBegin, intEnd);Object [] Char = {'a','b','f','g','h','i'};int charBegin = 0;int charEnd = Char.length -1;System.out.println("Char 数据初始结果为"+Arrays.toString(Char));forUse.quickSortForObject(Char, charBegin, charEnd);Student stu1 = new Student(1, "harry", "123");Student stu3 = new Student(3, "jenny", "123");Student stu4 = new Student(4, "mike", "123");Student stu2 = new Student(2, "david", "123");Object [] Stu = {stu2,stu1,stu4,stu3};int stuBegin = 0 ; int stuEnd = Stu.length -1;System.out.println("Student 数据初始结果为"+Arrays.toString(Stu));forUse.quickSortForObject(Stu, stuBegin, stuEnd);}}



最后执行出来的结果:

Int 数据初始结果为[1, 2, 3, 43, 23, 12, 12]
第1次调整结果: [1, 2, 3, 43, 23, 12, 12]
第2次调整结果: [1, 2, 3, 43, 23, 12, 12]
第3次调整结果: [1, 2, 3, 43, 23, 12, 12]
第4次调整结果: [1, 2, 3, 43, 23, 12, 12]
第5次调整结果: [1, 2, 3, 43, 23, 12, 12]
第6次调整结果: [1, 2, 3, 43, 23, 12, 12]
第7次调整结果: [1, 2, 3, 43, 23, 12, 12]
第8次调整结果: [1, 2, 3, 43, 23, 12, 12]
第9次调整结果: [1, 2, 3, 43, 23, 12, 12]
第10次调整结果: [1, 2, 3, 43, 23, 12, 12]
第11次调整结果: [1, 2, 3, 43, 23, 12, 12]
第12次调整结果: [1, 2, 3, 43, 23, 12, 12]
第13次调整结果: [1, 2, 3, 43, 23, 12, 12]
第14次调整结果: [1, 2, 3, 43, 23, 12, 12]
第15次调整结果: [1, 2, 3, 43, 23, 12, 12]
第16次调整结果: [1, 2, 3, 43, 23, 12, 12]
第17次调整结果: [1, 2, 3, 43, 23, 12, 12]
第18次调整结果: [1, 2, 3, 43, 23, 12, 12]
第19次调整结果: [1, 2, 3, 12, 23, 12, 12]
第20次调整结果: [1, 2, 3, 12, 23, 12, 12]
第21次调整结果: [1, 2, 3, 12, 23, 12, 12]
第22次调整结果: [1, 2, 3, 12, 23, 12, 43]
第23次调整结果: [1, 2, 3, 12, 23, 12, 43]
第24次调整结果: [1, 2, 3, 12, 23, 12, 43]
第25次调整结果: [1, 2, 3, 12, 23, 12, 43]
第26次调整结果: [1, 2, 3, 12, 12, 12, 43]
第27次调整结果: [1, 2, 3, 12, 12, 23, 43]
Char 数据初始结果为[a, b, f, g, h, i]
第1次调整结果: [a, b, f, g, h, i]
第2次调整结果: [a, b, f, g, h, i]
第3次调整结果: [a, b, f, g, h, i]
第4次调整结果: [a, b, f, g, h, i]
第5次调整结果: [a, b, f, g, h, i]
第6次调整结果: [a, b, f, g, h, i]
第7次调整结果: [a, b, f, g, h, i]
第8次调整结果: [a, b, f, g, h, i]
第9次调整结果: [a, b, f, g, h, i]
第10次调整结果: [a, b, f, g, h, i]
第11次调整结果: [a, b, f, g, h, i]
第12次调整结果: [a, b, f, g, h, i]
第13次调整结果: [a, b, f, g, h, i]
第14次调整结果: [a, b, f, g, h, i]
第15次调整结果: [a, b, f, g, h, i]
第16次调整结果: [a, b, f, g, h, i]
第17次调整结果: [a, b, f, g, h, i]
第18次调整结果: [a, b, f, g, h, i]
第19次调整结果: [a, b, f, g, h, i]
第20次调整结果: [a, b, f, g, h, i]
Student 数据初始结果为[Id=2, name=david, passwd=123, Id=1, name=harry, passwd=123, Id=4, name=mike, passwd=123, Id=3, name=jenny, passwd=123]
第1次调整结果: [Id=2, name=david, passwd=123, Id=1, name=harry, passwd=123, Id=4, name=mike, passwd=123, Id=3, name=jenny, passwd=123]
第2次调整结果: [Id=2, name=david, passwd=123, Id=1, name=harry, passwd=123, Id=4, name=mike, passwd=123, Id=3, name=jenny, passwd=123]
第3次调整结果: [Id=1, name=harry, passwd=123, Id=1, name=harry, passwd=123, Id=4, name=mike, passwd=123, Id=3, name=jenny, passwd=123]
第4次调整结果: [Id=1, name=harry, passwd=123, Id=2, name=david, passwd=123, Id=4, name=mike, passwd=123, Id=3, name=jenny, passwd=123]
第5次调整结果: [Id=1, name=harry, passwd=123, Id=2, name=david, passwd=123, Id=3, name=jenny, passwd=123, Id=3, name=jenny, passwd=123]
第6次调整结果: [Id=1, name=harry, passwd=123, Id=2, name=david, passwd=123, Id=3, name=jenny, passwd=123, Id=4, name=mike, passwd=123]



1 0
原创粉丝点击