每天搞算法与数据结构 ------每日更新

目录

一、排序系列

1.选择排序

2.冒泡排序

3.插入排序

二、LeetCode系列从第一题开始

1.两数之和

三、数据结构



一、排序系列

1.选择排序

规律:每次找后面最小的值和前面交换,直到结束。

第1趟:i=0。找出a[1...5]中的最小值a[3]=10,然后将a[0]和a[3]互换。 数组变化:20,40,30,10,60,50 -- > 10,40,30,20,60,50

第2趟:i=1。找出a[2...5]中的最小值a[3]=20,然后将a[1]和a[3]互换。 数组变化:10,40,30,20,60,50 -- > 10,20,30,40,60,50

第3趟:i=2。找出a[3...5]中的最小值,由于该最小值大于a[2],该趟不做任何处理。

第4趟:i=3。找出a[4...5]中的最小值,由于该最小值大于a[3],该趟不做任何处理。

第5趟:i=4。交换a[4]和a[5]的数据。 数组变化:10,20,30,40,60,50 -- > 10,20,30,40,50,60

/******************************************************************************

Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl,
C#, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog.
Code, Compile, Run and Debug online from anywhere in world.

*******************************************************************************/
public class Main
{
  public static void selectSort (int[]arr)
  {
    if (arr == null || arr.length < 2)
      {
	return;
      }
    int N = arr.length;
    for (int i = 0; i < N; i++)
      {
	int minValueIndex = i;
	for (int j = i + 1; j < N; j++)
	  {
	    minValueIndex = arr[j] < arr[minValueIndex] ? j : minValueIndex;
	  }
	swap (arr, i, minValueIndex);
      }
  }

/*
交换
*/
  public static void swap (int[]arr, int i, int j)
  {
    int temp = arr[j];
    arr[j] = arr[i];
    arr[i] = temp;
  }

  public static void printArray (int[]arr)
  {
    for (int i = 0; i < arr.length; i++)
      {
	System.out.print (arr[i] + " ");
      }
    System.out.println ();
  }
  public static void main (String[]args)
  {
    int[] arr = { 2, 5, 488, 1, 0, 85748, 10, 99 };
    printArray (arr);
    selectSort (arr);
    printArray (arr);
  }
}

2.冒泡排序

规律:相邻的数依次比较找出最大值排到最后。

/******************************************************************************

Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl,
C#, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog.
Code, Compile, Run and Debug online from anywhere in world.

*******************************************************************************/
public class Main
{
	public static void main(String[] args) {
	    int[] arr = {20,40,30,10,60,50};
		printArray(arr);
		bubbleSort(arr);
		printArray(arr);
	}
	

	/**
	 * 冒泡函数
	 */ 
	 public static void bubbleSort(int[] arr){
	     if(arr == null ||arr.length < 2){
	         return;
	     }
	   //  0 ~ n-1
	   //  0 ~ n-2
	   //  0 ~ n-3
	   //  0 ~ n-4
	   //  0 ~ end
        int N = arr.length;  
        for (int end = N-1;end >= 0 ;end-- ){ // 每次的交换次数
            for(int secend = 1;secend <= end;secend++){ // 每次交换的具体事物 
                if(arr[secend-1] > arr[secend]){
                    swap(arr,secend-1,secend);
                }
            }
        } 

	 }
	 
	 /**
	  * 交换
	  */
	 public static void swap(int[] arr, int i,int j){
	    int temp = arr[i];
	    arr[i] = arr[j];
	    arr[j] = temp;
	 }
	 
	 /**
	  * 打印数组
	  */
	  
	 public static void printArray(int[] arr){
	     for(var a : arr){
	         System.out.print(a+" ");
	     }
	     System.out.println();
	 }
	
}

3.插入排序

规律:从第二位置开始前面变有序,再从第三位开始前面变有序,直到end的前面全部有序。

/******************************************************************************

Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl,
C#, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog.
Code, Compile, Run and Debug online from anywhere in world.

*******************************************************************************/
public class Main
{
	public static void main(String[] args) {
	    int[] arr = {20,40,30,10,60,50};
		printArray(arr);
		insertSort2(arr);
		printArray(arr);
	}
	

	/**
	 * 插入函数1
	 */ 
	 public static void insertSort1(int[] arr){
	     if(arr == null ||arr.length < 2){
	         return;
	     }
	   //  0 ~ 1 范围中有序
	   //  0 ~ 2 范围中有序
	   //  0 ~ 3 范围中有序
	   //  0 ~ 4 范围中有序
	   //  0 ~ 5 范围中有序
	   //  0 ~ n-1 范围中有序
	   int N = arr.length;
	   for(int end = 1; end < N; end++){
	       int newNumberIndex = end;
	       while(newNumberIndex - 1 >= 0 && arr[newNumberIndex-1] > arr[newNumberIndex]){
	           swap(arr,newNumberIndex-1,newNumberIndex);
	           newNumberIndex--;
	       }
	   }
	 }
	 
	 /**
	 * 插入函数2
	 */ 
	 public static void insertSort2(int[] arr){
	     if(arr == null ||arr.length < 2){
	         return;
	     }
	   //  0 ~ 1 范围中有序
	   //  0 ~ 2 范围中有序
	   //  0 ~ 3 范围中有序
	   //  0 ~ 4 范围中有序
	   //  0 ~ 5 范围中有序
	   //  0 ~ n-1 范围中有序
	   int N = arr.length;
	   for(int end = 1; end < N; end++){
	       int newNumberIndex = end;
	       for(int pre = end - 1; pre >= 0 && arr[pre] > arr[pre + 1]; pre-- ){
	           swap(arr,pre,pre+1);
	       }
	   }
	 }
	 /**
	  * 交换
	  */
	 public static void swap(int[] arr, int i,int j){
	    int temp = arr[i];
	    arr[i] = arr[j];
	    arr[j] = temp;
	 }
	 
	 /**
	  * 打印数组
	  */
	  
	 public static void printArray(int[] arr){
	     for(var a : arr){
	         System.out.print(a+" ");
	     }
	     System.out.println();
	 }
	
}

二、LeetCode系列从第一题开始

1.两数之和

class Solution {
    public int[] twoSum(int[] nums, int target) {
        int n = nums.length;
        for(int i = 0;i<n;++i ){
           for(int j = i+1;j<n;++j){
               if(nums[i]+nums[j] == target){
                  return new int[]{i,j};
               }
           }
        }
         return new int[]{1,33};
    }
}

三、数据结构

组成可以大概分为:连续结构、跳转结构或者是两者的结合。

eg::::::::

数组:连续性结构,寻址容易,加减数据不容易。

链表:跳转性结构,加减数据容易,寻址难。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值