专题 查找与排序的Java代码实现(一)
专题 查找与排序的Java代码实现(一)
查找(Searching)
线性查找(linear search)
属于无序查找算法,适合于存储结构为顺序存储或链接存储的线性表。
基本思想:从数据结构线形表的一端开始,顺序扫描,依次将扫描到的结点关键字与给定值k相比较,若相等则表示查找成功;若扫描结束仍没有找到关键字等于k的结点,表示查找失败。
时间复杂度:O(n)
具体代码:
//-------------------------------------------------------------------------
// Searches the specified array of objects using a linear search 线性查找
// algorithm. Returns null if the target is not found.
//-------------------------------------------------------------------------
public static Comparable linearSearch (Comparable[] data,
Comparable target) {
Comparable result = null;
int index = 0;
while (result == null && index < data.length){
if (data[index].compareTo(target) == 0)
result = data[index];
index++;
}
return result;
}
二分查找(binary search)
属于有序查找算法,元素必须是有序的,如果是无序的则要先进行排序操作。
基本思想:用给定值k先与中间结点的关键字比较,中间结点把线形表分成两个子表,若相等则查找成功;若不相等,再根据k与该中间结点关键字的比较结果确定下一步查找哪个子表,这样递归进行,直到查找到或查找结束发现表中没有这样的结点。
时间复杂度:O(log2n)
具体代码:
//--------------------------------------------------------------------------
// Searches the specified array of objects using a binary search 二分查找
// algorithm. Returns null if the target is not found.
//--------------------------------------------------------------------------
public static Comparable BinarySearch(Comparable[] data,
Comparable target){
Comparable result = null;
int first = 0, last = data.length-1, mid;
while (result == null && first <= last){
mid = (first + last) / 2; // determine midpoint
if (data[mid].compareTo(target)==0)
result = data[mid];
else
if (data[mid].compareTo(target) == 0)
result = mid - 1;
else
first = mid + 1;
}
return result;
}
排序(Sorting)
选择排序(selection sort)
基本思想:对一个数列进行排序时,每次从剩余的序列中挑选出最小的记录,放到序列开始位置,以此类推,直到数列的所有数字都已经放到最终位置为止。
时间复杂度:O(n^2)
具体代码:
//-----------------------------------------------------------------
// Sorts the specified array of integers using the selection
// sort algorithm.
//-----------------------------------------------------------------
public static void selectionSort (Comparable[] data) {
int min;
for (int index = 0; index < data.length-1; index++) {
min = index;
for (int scan = index+1; scan < data.length; scan++)
if (data[scan].compareTo(data[min]) < 0)
min = scan;
swap (data, min, index);
}
}
//-----------------------------------------------------------------
// Swaps two elements in the specified array.
//-----------------------------------------------------------------
private static void swap (Comparable[] data, int index1, int index2)
{
Comparable temp = data[index1];
data[index1] = data[index2];
data[index2] = temp;
}
插入排序(insertion sort)
基本思想:每次从数列中取一个还没有取出过的数,并按照大小关系插入到已经取出的数中使得已经取出的数仍然有序。
时间复杂度:O(n^2)
具体代码:
//-----------------------------------------------------------------
// Sorts the specified array of objects using an insertion
// sort algorithm.
//-----------------------------------------------------------------
public static void insertionSort (Comparable[] data)
{
for (int index = 1; index < data.length; index++)
{
Comparable key = data[index];
int position = index;
// Shift larger values to the right
while (position > 0 && data[position-1].compareTo(key) > 0)
{
data[position] = data[position-1];
position--;
}
data[position] = key;
}
}
冒泡排序(bubble sort)
属于交换排序
基本思想:经过一趟冒泡排序之后,序列中最大的记录到了序列最后,而较小的记录位置均向前移动了
时间复杂度:O(n^2)
具体代码:
//-----------------------------------------------------------------
// Sorts the specified array of objects using a bubble sort
// algorithm.
//-----------------------------------------------------------------
public static void bubbleSort (Comparable[] data)
{
int position, scan;
for (position = data.length - 1; position >= 0; position--)
{
for (scan = 0; scan <= position - 1; scan++)
if (data[scan].compareTo(data[scan+1]) > 0)
swap (data, scan, scan+1);
}
}
快速排序(quick sort)
属于交换排序,快速排序是对冒泡排序的一种改进。
基本思想:将待排序序列分成两部分,其中一部分的记录都比另一部分的记录小,随后分别对这两部分再分成两部分,使一部分的记录都小于另一部分,如此反复最终使整个序列最终有序。
时间复杂度:O(n*log2n)
具体代码:
//-----------------------------------------------------------------
// Sorts the specified array of objects using the quick sort
// algorithm.
//-----------------------------------------------------------------
public static void quickSort (Comparable[] data, int min, int max)
{
int pivot;
if (min < max)
{
pivot = partition (data, min, max); // make partitions
quickSort(data, min, pivot-1); // sort left partition
quickSort(data, pivot+1, max); // sort right partition
}
}
//-----------------------------------------------------------------
// Creates the partitions needed for quick sort.
//-----------------------------------------------------------------
private static int partition (Comparable[] data, int min, int max)
{
// Use first element as the partition value
Comparable partitionValue = data[min];
int left = min;
int right = max;
while (left < right)
{
// Search for an element that is > the partition element
while (data[left].compareTo(partitionValue) <= 0 && left < right)
left++;
// Search for an element that is < the partitionelement
while (data[right].compareTo(partitionValue) > 0)
right--;
if (left < right)
swap(data, left, right);
}
// Move the partition element to its final position
swap (data, min, right);
return right;
}
归并排序(merge sort)
基本思想:重复调用归并算法,首先将单个记录视为一个有序序列,然后不断将相邻的两个有序序列合并得到新的有序序列,如此反复,最后得到一个整体有序的序列
时间复杂度:O(n*log2n)
具体代码:
//-----------------------------------------------------------------
// Sorts the specified array of objects using the merge sort
// algorithm.
//-----------------------------------------------------------------
public static void mergeSort (Comparable[] data, int min, int max)
{
if (min < max)
{
int mid = (min + max) / 2;
mergeSort (data, min, mid);
mergeSort (data, mid+1, max);
merge (data, min, mid, max);
}
}
//-----------------------------------------------------------------
// Sorts the specified array of objects using the merge sort
// algorithm.
//-----------------------------------------------------------------
public static void merge (Comparable[] data, int first, int mid,
int last)
{
Comparable[] temp = new Comparable[data.length];
int first1 = first, last1 = mid; // endpoints of first subarray
int first2 = mid+1, last2 = last; // endpoints of second subarray
int index = first1; // next index open in temp array
// Copy smaller item from each subarray into temp until one
// of the subarrays is exhausted
while (first1 <= last1 && first2 <= last2)
{
if (data[first1].compareTo(data[first2]) < 0)
{
temp[index] = data[first1];
first1++;
}
else
{
temp[index] = data[first2];
first2++;
}
index++;
}
// Copy remaining elements from first subarray, if any
while (first1 <= last1)
{
temp[index] = data[first1];
first1++;
index++;
}
// Copy remaining elements from second subarray, if any
while (first2 <= last2)
{
temp[index] = data[first2];
first2++;
index++;
}
// Copy merged data into original array
for (index = first; index <= last; index++)
data[index] = temp[index];
}
专题 查找与排序的Java代码实现(一)的更多相关文章
- 排序算法Java代码实现(一)—— 选择排序
以下几篇随笔都是记录的我实现八大排序的代码,主要是贴出代码吧,讲解什么的都没有,主要是为了方便我自己复习,哈哈,如果看不明白,也不要说我坑哦! 本片分为两部分代码: 常用方法封装 排序算法里需要频繁使 ...
- 排序算法Java代码实现(三)—— 插入排序 和 希尔排序
因为希尔排序的核心思想是插入排序,所以本篇将两篇排序一起记录 本篇内容: 插入排序 希尔排序 (一)插入排序 算法思想: 把n个待排序的元素看成一个有序表和一个无序表,开始时有序表中只有一个元素,无序 ...
- 排序算法Java代码实现(四)—— 归并排序
本篇内容: 归并排序 归并排序 算法思想: 将两个或两个以上的有序表合并成一个新的有序表, 即把待排序序列分成若干个子序列,每个子序列是有序的,然后在把有序子序列合并为整体有序序列. 此算法分为两步: ...
- 排序算法Java代码实现(六)—— 堆排序
本片内容: 堆排序 堆排序 最大堆: 二叉堆是完全二叉树或者是近似完全二叉树, 当父结点的键值总是大于或等于任何一个子节点的键值时为最大堆.(父节点大于任何一个子节点) 算法思想: 把n个元素建立最大 ...
- 排序算法Java代码实现(五)—— 快速排序
本篇内容: 快速排序 快速排序 算法思想: 通过一趟排序将要排序的数据分割成独立的两部分, 其中一部分的所有数据都比另外一部分的所有数据都要小, 然后再按此方法对这两部分数据分别进行快速排序, 整个排 ...
- Java - 选择性排序 PHP || Java 代码对比
int [] array1 = {1,3,5,7,9,10,2,15,154,10,2,188,200};//定义一个数组,内容为混乱大小 int index = 0;//定义一个最大值或最小值的位置 ...
- 八大排序算法java代码
1.冒泡排序 public static void main(String[] args) { int[] arr = {1,4,2,9,5,7,6}; System.out.println(&quo ...
- 排序算法Java代码实现(二)—— 冒泡排序
本篇内容: 冒泡排序 冒泡排序 算法思想: 冒泡排序的原理是:从左到右,相邻元素进行比较. 每次比较一轮,就会找到序列中最大的一个或最小的一个.这个数就会从序列的最右边冒出来. 代码实现: /** * ...
- 20172302 《Java软件结构与数据结构》实验三:查找与排序实验报告
课程:<Java软件结构与数据结构> 班级: 1723 姓名: 侯泽洋 学号:20172302 实验教师:王志强老师 实验日期:2018年11月19日 必修/选修: 必修 实验内容 (1) ...
随机推荐
- annotation的概念及其作用
概念 能够添加到 Java 源代码的语法元数据.类.方法.变量.参数.包都可以被注解,可用来将信息元数据与程序元素进行关联.Annotation 中文常译为“注解”. 作用 标记,用于告诉编译器一些信 ...
- macbook 显示所有文件夹
在macbook终端执行如下代码: 1. 设置打开所有的文件 defaults write com.apple.finder AppleShowAllFiles -bool true 2. 关闭之前打 ...
- CDI的分析
CDI是一组服务,它们一起使用,使开发人员可以轻松地在Web应用程序中使用企业bean和JavaServer Faces技术.CDI设计用于有状态对象,还有许多更广泛的用途,允许开发人员以松散耦合但类 ...
- 浅谈一下mshta在CVE-2017-11882里的命令构造
Evi1cg同学前不久放出CVE-2017-11882的一个 python利用脚本,地址在https://github.com/Ridter/CVE-2017-11882/,不过其中一个版本里边有一个 ...
- Linux双线双网卡双IP双网关设置方法
机房上架了一台测试机,系统是Ubuntu 9.04 X64的系统,母机IBM X336机器.用户需求是双线,故采用一个网卡配置电信地址,另一个网卡配置联通地址,安装好系统后配置好IP发现联通地址和电信 ...
- zabbix之 自动发现磁盘io util 监控
一.iostat Zabbix并没有提供模板来监控磁盘的IO性能,所以我们需要自己来创建一个.iostat主要用于监控系统设备的IO负载情况,iostat首次运行时显示自系统启动开始的各项统计信息,之 ...
- xgboost实例代码
# -*- coding: utf-8 -*- import xgboost as xgb import csv import jieba jieba.load_userdict('wordDict. ...
- c# 数据存储过程
什么是存储过程? 用某百科的话来说就是一堆为了完成某一功能或者某些功能的SQL语句的集合,而数据库则会将这些存储过程的方法存储到数据库中去. 优点: 1.可重用并且效率高:存储过程经过一次编译后不需要 ...
- JavaScript获取mp4文件MIME编码格式,用于判读是否是h.264,解决在线播放只有声音问题
测试网址:https://gpac.github.io/mp4box.js/test/filereader.html js库:mp4box.js 不能在线播放的:audio/mp4; codecs=& ...
- MIDAS.dll 出错时 (Error loading MIDAS.DLL.)
DELPHI 写的程序会出 ---------------------------Pmain---------------------------Error loading MIDAS.DLL.--- ...