时间复杂度
有序数组 | 无序数组 | |
---|---|---|
增 | n/2+log2nlog2n | 1 |
删 | n/2+log2nlog2n | N |
改 | log2nlog2n | N/2 |
查 | log2nlog2n | N/2 |
插入 | n/2+log2nlog2n | N/2 |
在查找比较频繁的情况下,可以考虑使用有序数组,其它情况一律使用无序数组。
数组共有的缺点是插入比较慢,删除比较慢,新增时无序快,有序慢。然后大小固定。
链表查找指定位置的元素,时间复杂度为n/2,查找指定元素也是n/2.
数组查找指定位置的元素,时间复杂度为常量1,查找指定元素为n/2.
查找的话,看是根据位置查找,还是根据元素查找,后者的话,二种方式差距不大。
数组删除指定位置的元素,时间复杂度为n/2,删除指定元素为n
链表删除指定位置的元素,时间复杂度为n/2,删除指定元素为n/2.
但是这两个虽然都是n/2,但是链表的删除更快,因为链表只需要找到元素,移除就行了,而数组是要移动后面的元素的。所以链表的删除更快,但是如果一直都是删除末尾的元素的话,数组比链表好用。
插入的话,同删除一样。
修改的话,数组有绝对优势。当然,如果是根据元素修改而不是指定位置,二者差别不大(数组还是占优)。
下面是排序算法:
冒泡排序
public static void maoPaoSort(int[] array){
for (int i=array.length-1;i>1;i--){
for (int j=0;j<i;j++){
if (array[j]>array[i]){
int temp=array[j];
array[j]=array[i];
array[i]=temp;
}
}
}
}
仿冒泡:
public void maoPaoSort(int[] array){
for (int i=0;i<array.length-1;i++){
for (int j=i+1;j<array.length;j++){
if (array[j]<array[i]){
int temp=array[j];
array[j]=array[i];
array[i]=temp;
}
}
}
}
`
选择排序是冒泡排序的一个改进,一般来说选择排序的速度比冒泡排序要快,因为它的交换字数更少。它们的时间复杂度是一样的。
public static void xuanZeSort(int[] array){
for (int i=array.length-1;i>1;i--){
int bigger=i;
for (int j=0;j<i;j++){
if (array[j]>array[bigger]){
bigger=j;
}
}
swap(array,i,bigger);
}
}
插入排序是比冒泡排序和选择排序更快的一种算法,实现也复杂一些。
public static void chaRuSort(int[] array){
for (int i=1;i<array.length;i++){
int temp=array[i];
int j=i;
while (j>0&&array[j-1]>temp){
array[j]=array[j-1];
j--;
}
array[j]=temp;
}
}
递归版插入排序:
public static void insertSortRecursion(int[] array,int n){
int temp=array[n];
int i=n;
for (;i>0;i--){
if (array[i-1]>temp){
array[i]=array[i-1];
}else {
break;
}
}
array[i]=temp;
}
public static void diguiChaRuPaiXu(int[] array,int n){
if (n<array.length){
insertSortRecursion(array,n);
diguiChaRuPaiXu(array,n+1);
}
}
public static void main(String[] args) {
Random random=new Random(47);
int array[]=new int[10];
for (int i=0;i<array.length;i++){
array[i]=random.nextInt();
}
diguiChaRuPaiXu(array,0);
// guiBing(array,0,array.length-1);
//intSortGuiBing(array,0,0,1);
for (int a:array){
System.out.print(a+"__");
}
}
结果:
-2076178252__-2014573909__-1812486437__-1172028779__-1128074882__229403722__688081923__809509736__1717241110__1791060401__
希尔排序:它是插入排序的升级版
特点是在最好与最坏的情况下,时间复杂度都差不多,也就是稳定。他比冒泡,插入、选择排序都要快些。不如快速排序。
public class Xier {
public static void main(String[] args) {
ArraySh arraySh=new ArraySh(10);
Random random=new Random();
for (int i=0;i<10;i++){
arraySh.insert(random.nextInt());
}
arraySh.display();
arraySh.shellSort();
arraySh.display();
}
}
class ArraySh{
private long[] theArray;
private int nElems;
public ArraySh(int max){
theArray=new long[max];
nElems=0;
}
public void insert(long value){
if (nElems==theArray.length){
return;
}
theArray[nElems]=value;
nElems++;
}
public void display(){
System.out.println("A=");
for (int j=0;j<nElems;j++){
System.out.println(theArray[j]+" ");
}
System.out.println(" ");
}
public void shellSort(){
int inner,outer;
int h=1;
long temp;
while (h<nElems/3){
h=h*3+1;
}
while(h>0){
for (outer=h;outer<nElems;outer++){
temp=theArray[outer];
inner=outer;
while (inner>h-1 && theArray[inner-h]>=temp){
theArray[inner]=theArray[inner-h];
inner-=h;
}
theArray[inner]=temp;
}
h=(h-1)/3;
}
}
}
归并排序法(时间复杂度nlogn):
public static void main(String[] args) {
Random random=new Random(47);
int array[]=new int[3];
for (int i=0;i<array.length;i++){
array[i]=random.nextInt();
}
guiBing(array,0,array.length-1);
//intSortGuiBing(array,0,0,1);
for (int a:array){
System.out.print(a+"__");
}
}
结果:
-2014573909__-1172028779__1717241110__
public static void guiBing(int[] array,int start,int end){
if (start>=end)
return;
int boundary=(start+end)/2;
guiBing(array,start,boundary);
guiBing(array,boundary+1,end);
intSortGuiBing(array,start,boundary,end);
}
/**
* 排序数组
* @param array 待排序数组
* @param array1Start 有序部分1的起始位置
* @param endAndStart 有序部分1的终止位置,也是有序部分2 的起始位置
* @param array2End 有序部分2的终止位置
*/
public static void intSortGuiBing(int[] array, int array1Start, int endAndStart, int array2End){
int len1=endAndStart-array1Start+1;
int len2=array2End-endAndStart;
int[] array1= new int[len1];
int[] array2= new int[len2];
//复制数组
for (int i=array1Start;i<=array2End;i++){
if (i<=endAndStart){
array1[i-array1Start]=array[i];
}
else {
array2[i-endAndStart-1]=array[i];
}
}
int temp1=0;
int temp2=0;
//合并数组
while (array1Start<=array2End){
if (temp1<len1 && temp2<len2 ) {
if (array1[temp1]<=(array2[temp2]))
array[array1Start++] = array1[temp1++];
else
array[array1Start++] = array2[temp2++];
}
else if (temp1==len1){
array[array1Start++] = array2[temp2++];
}
else {
array[array1Start++] = array1[temp1++];
}
}
}
快速排序法
算法思想:基于分治的思想,是冒泡排序的改进型。首先在数组中选择一个基准点,然后分别从数组的两端扫描数组,设两个指示标志(lo指向起始位置,hi指向末尾),首先从后半部分开始,如果发现有元素比该基准点的值小,就交换lo和hi位置的值,然后从前半部分开始扫秒,发现有元素大于基准点的值,就交换lo和hi位置的值,如此往复循环,直到lo>=hi,然后把基准点的值放到hi这个位置。一次排序就完成了。以后采用递归的方式分别对前半部分和后半部分排序,当前半部分和后半部分均有序时该数组就自然有序了。
public void kuaiSuSort(int[] arrary,int lef,int right){
if (lef>=right)
return;
int cutOff=fenge(arrary,lef,right);
kuaiSuSort(arrary,lef,cutOff-1);
kuaiSuSort(arrary, cutOff+1,right);
}
public int fenge(int[] array,int lef,int right){
int temp=array[lef];
while (lef<right){
while (array[right]>=temp && lef<right){
right--;
}
array[lef]=array[right];
// array[right]=temp;
while (array[lef]<=temp && lef<right){
lef++;
}
array[right]=array[lef];
}
array[lef]=temp;
return right;
}
快速排序在序列中元素很少时,效率将比较低.
三向切分快速排序法,适合重复元素多的数组:
public static void sanXiang(int[] array,int left,int right){
if (left>=right){
return;
}
int begin=left;
int end=right;
int temp=left+1;
int tempValue=array[left];
while (temp<=right){
if (array[temp]>tempValue){
int tt= array[right];
array[right--]=array[temp];
array[temp]=tt;
}
else if (array[temp]<tempValue){
int tt=array[left];
array[left]=array[temp];
array[temp]=tt;
left++;
temp++;
}
else {
temp++;
}
}
sanXiang(array,begin,left);
sanXiang(array,right+1,end);
}