算法效率
- 时间复杂度:
代码执行的次数 是关于 问题规模n的函数,表示算法执行时间随数据规模的增大而增大的趋势。
O:渐进时间复杂度
o:最坏时间复杂度
化简方法:
(1)常数项化简为1
(2)只保留n的最高次方(关于n)(系数化简为1)
例:1/2n^2+3n+1 :O(n^2)
常用:
次数跟问题规模无关: O(1)
遍历1次(1个for循环):O(n)
双层(嵌套)for循环: O(n^2)
二分(折半)查找: O(log n) - 空间复杂度:
需要额外开辟的内存空间关于内存规模n的函数
与问题规模无关:O(1)
扩容操作 O(n)
基础排序
奇偶排序问题
奇数在前偶数在后:时间复杂度O(n)
import java.util.Arrays;
public class Test1 {
public static void sort(int[]arr){
if(arr==null){
return;
}
int temp = arr[0];
int i=0,j=arr.length-1;
while (i<j){
while (i<j&&arr[j]%2==0){//找到奇数为止
j--;
}//j标记到奇数位置
arr[i]=arr[j];
while (i<j&&arr[i]%2==1){
i++;
}
arr[j]=arr[i];
}
arr[i]=temp;
}
public static void main(String[] args) {
int[] arr = {3,2,1,6,4,5,0};
sort(arr);
System.out.println(Arrays.toString(arr));
}
}
二分查找
前提:数组必须有序
eg: 1 3 5 7 9 10 value:9 ,找出给定值
时间复杂度:O(logn)
推导过程:
n/2/2/2/2… = 1
n/2^次数 = 1
2^次数 = n
次数 = log2 n
思路:
定义两个下标 low、high,分别从数组首尾位置开始,每次取两者中间位置 mid,mid与value比较,若 mid > value,high走至 mid-1位置(反之 low走至 mid+1位置),继续取中间进行比较…若两个下标错位,则不存在value,若存在,mid位置为value。
优化:(low+high)/ 2 —>(low+high)>> 1
若 low+high超出整型范围:((high-low)>>1)+ low
递归、非递归实现
public class Demo1 {
public static int binarySearch(int[] brr, int value){
if(brr==null){
return -1;
}
int left=0,right= brr.length-1;
while (left<=right){
int midIndex=((right-left)>>1)+left;
if(brr[midIndex]==value){
return midIndex;
}else if(brr[midIndex]<value){
left=midIndex+1;
}else{
right=midIndex-1;
}
}
return -1;
}
public static void main(String[] args) {
int[] brr = {1,3,5,7,9,10};
int b=binarySearch(brr,9);
System.out.println(b);
}
}
冒泡排序
平均时间复杂度 O(n^2)
最优时间复杂度 O(n)
import java.util.Arrays;
public class Demo1{
public static void swap(int[] arr,int index1,int index2){
if(arr==null||index1<0||index2<0){
return;
}
int temp = arr[index1];
arr[index1] = arr[index2];
arr[index2] = temp;
}
public static void replace(int[] arr){
if(arr==null||arr.length==1){
return;
}
boolean flag;
for(int i=0;i<arr.length;i++){//i控制趟数
flag = false;
for(int j=0;j<arr.length-1-i;j++){
if(arr[j]>arr[j+1]){
swap(arr,j,j+1);
flag = true;
}
}
if(!flag){
break;
}
}
}
public static void main(String[] args) {
int[] arr = {2,4,1,7,3,9};
replace(arr);
System.out.println(Arrays.toString(arr));
}
}
选择排序
从当前待排序序列中,选择最小值,最大值和待排序序列首、末位进行交换。
平均时间复杂度O(n^2)
import java.util.Arrays;
public class Demo{
public static void swap(int[] arr,int index1,int index2){
if(arr==null||index1<0||index2<0){
return;
}
int temp = arr[index1];
arr[index1] = arr[index2];
arr[index2] = temp;
}
public static void replace1(int[] brr){
if(brr==null||brr.length==1){
return;
}
int i=0,j= brr.length-1;
while (i<j){
int minIndex = i;
int maxIndex = i;
for(int z=i;z<=j;z++){
if (brr[minIndex]>brr[z]){
minIndex = z;
}
if (brr[maxIndex]<brr[z]){
maxIndex = z;
}
}
int b = brr[maxIndex];
swap(brr,minIndex,i);
if(b==brr[maxIndex]){
swap(brr,maxIndex,j);
j--;
}
i++;
}
}
public static void main(String[] args) {
int[] brr = {3,0};
replace1(brr);
System.out.println(Arrays.toString(brr));
}
}
插入排序
import java.util.Arrays;
public class Demo1{
public static void insert(int[] arr){
if(arr==null||arr.length==1){
return;
}
int temp;
for(int i=0;i<arr.length-1;i++){
for(int j=i+1;j>0;j--){
if(arr[j-1]>arr[j]){
temp=arr[j-1];
arr[j-1]=arr[j];
arr[j]=temp;
}
}
}
}
public static void main(String[] args) {
int[] arr = {1,7,8,2,0,9,5};
insert(arr);
System.out.println(Arrays.toString(arr));
}
}
字符替换
- 给定字符串String str = “i am a student”,采用char[]操作,将一个指定字符替换为指定个数的字符;
时间复杂度:O(n)
空间复杂度:O(n)
public class Test1 {
public static int getCharCount(char[] ch,char aim){
if(ch==null){
return -1;
}
int count = 0;
for(int i=0;i<ch.length;i++){
if(ch[i]==aim){
count++;
}
}
return count;
}
public static String insert(String str,char src,int num,char aim){
if(str==null){
return null;
}
char[] ch = str.toCharArray();
//统计空格个数
int count=getCharCount(ch,src);
int srclength = ch.length;
//扩容
ch = Arrays.copyOf(ch,ch.length+count*(num-1));
int i = srclength-1;//i遍历原始数据个数
int j = ch.length-1;
while (i>=0){
if(ch[i]!=src){
ch[j--] = ch[i];
}else{
for(int z=0;z<num;z++){
ch[j--] = aim;
}
}
i--;
}
return String.valueOf(ch);
}
public static void main(String[] args) {
String str = "i am a student";
String s= insert(str,'a',3,'%');
System.out.println(s);
}
}
- 给定字符串 String str = “123”,转成整型 int 123
测试:String str = “-123” -> int:-123
str = “-±-123” -> int:-123
public class Test {
public static int replace(String str) {
int i=0,j=0,count=0,m=1;
for(;i<str.length();i++){
while(str.charAt(i)=='+'||str.charAt(i)=='-'){
if(str.charAt(i)=='-'){
count++;
}i++;
}
if(count%2!=0){m=-1;}
char ch = str.charAt(i);
int n = str.charAt(i)-'0';
j = (int)(j+n*Math.pow(10,str.length()-1-i));
}
return j*=m;
}
public static void main(String[] args) {
String str = "-+--123";
int j = replace(str);
System.out.println(j);
}
}