1-- Gang Wang time complexity algorithms and data structures and tables and bubble sorting order of selection and sorting

Getting a data structure and algorithm
data structure is a computer store, organize data. Data structure refers to the presence of one or more data elements of the particular relationship between each set. Algorithms + Data Structures = Programs. The quality of the program time complexity = + + space complexity scenarios.
1. Time Complexity: The number of times the key code is executed, such as the following code

 public void function1(){
    	for(int i=0;i<n;i++){
    		for(int j=0;j<n;j++){
    			//do someting
    		}
    		
    	}
    	for(int i=0;i<n;i++){
    		//do something
    	}
    		//do something 时间复杂度是1
    }

Time Complexity refers to do something, do something what we call critical code.
The time complexity for two cycles is o (n) = n ^ 2 ; for a cycle time complexity = n o (n); function1 so the above time complexity is o (n) = n ^ 2 + n + 1

We look at function2

public void function2() {
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				// do someting
			}
		}
	}

Obviously it's a complicated time o (n) = n ^ 2 ;
assuming that these two methods to solve the problem, then funtion1 and funtion2 algorithm which better it? You can set answer funtion2; Congratulations, you got it wrong. In fact, no one is better of the two algorithms divide their time complexity is equal. We assume that n approaches infinity when the amount of data an infinite number of times, in fact, in both cases are equal, we see time complexity is mainly to see n several parties.
2; spatial complexity;
codes we look at the value exchange;

        int a = 5;
		int b = 6;
		int t = a;
		a = b;
		b = t;

Space complexity are the three variables. We now make a small space.

        int a=15;
		int b=9;
		a=a^b;
		b=a^b;
		a=a^b;
		 

Here Insert Picture Description
Or binary arithmetic: 1 for true, 0 for false. Symbol is not the same when (a 1, a 0), the result is a true, when the same reference numerals (same two is 0, 1 or the same), the result is false 0.
If a, b two values are different, the result is an exclusive OR. If a, b values are the same, the exclusive OR result is 0.
Here Insert Picture Description
3, the order table;
linear form, comprising a sequential storage structure, such as ArrayList and Storage Structure. Table structure called sequential storage order table.

Here Insert Picture Description
We need to study the order of additions and deletions to change search table
(1) inquiry

public int search(int[] array,int a){
		for(int i=0;i<array.length;i++){
			//查询a是否在array里,是的话返回它的位置,否就返回-1
			if(array[i]==a){
				return i;
			}
		}
		  return -1;
	}

(2) Change: very straightforward modification value on the line. Here is not to write code.
(3) Insert:

Here Insert Picture Description

// 在第index个地址插入数据a
	public void insert(int[] array, int index,int a) {
		for (int i = array.length - 1; i > index; i--) {
			array[i] = array[i - 1];
		}
		array[index]=a;
	}

(4) Delete

// 在第index个地址删除数据
	public static void delete(int[] array, int index) {
		for (int i = index; i <array.length-1; i++) {
			array[i] =array[i+1] ;
		}
	}

4 bubble sort
Here Insert Picture Description
facie codes
of the first sorting round wording

public static void main(String[] args) {
		int[] a = new int[] { 1, 9, 6, 4, 8 };
		bubbleSort(a);
		for(int i=0;i<a.length;i++){
			System.out.print(a[i]+",");
             //输出1,6,4,8,9,
		}
		
	}

	// 第一轮排序
	public static void bubbleSort(int[] array) {
		for (int j = 0; j < array.length - 1; j++) {
			if (array[j] > array[j + 1]) {
				int temp = array[j];
				array[j] = array[j + 1];
				array[j + 1] = temp;
			}
		}

	}

9 can be seen already in the final surface. Here we are in the implementation cycle of 9 can be removed, because 9 is already sorted, and only the first row of data on it. The second sort for loop is n-1, for loop is the third n-2, and so on. Until 1;
the code is such a

// j<i中的i就是我们每一轮都要-1
	public static void bubbleSort(int[] array) {
		for (int i = array.length - 1; i > 0; i--) {
			for (int j = 0; j < i; j++) {
				if (array[j] > array[j + 1]) {
					int temp = array[j];
					array[j] = array[j + 1];
					array[j + 1] = temp;
				}
			}
		}

	}

We look time complexity, the first for loop, it is n times, for the second loop is n-1; and so on. Until last. Therefore, the time complexity is
n (n-1) / 2 .
We optimized under

public static void bubbleSort(int[] array) {
		for (int i = array.length - 1; i > 0; i--) {
			boolean flag = true;
			for (int j = 0; j < i; j++) {
				if (array[j] > array[j + 1]) {
					int temp = array[j];
					array[j] = array[j + 1];
					array[j + 1] = temp;
					flag = false;
				}
			}
			if (flag) {
				break;
			}
		}

	}

5, choose Sort:
Here Insert Picture Description

public static void main(String[] args) {
		int[] a = new int[] { 3, 1, 5, 8, 2, 9, 4, 6,7 };
		selectSort(a);
	}

	public static void selectSort(int[] array) {
		int index = 0;
		for (int j = 1; j < array.length; j++) {
			if (array[j] < array[index]) {
				index = j;
			}
		}
		System.out.print(index);//输出是4,正好是1的位置
	}

The code above we find that the index has been pointing minimum data. Here we have two values ​​are exchanged.

public static void selectSort(int[] array) {
		int index = 0;
		for (int j = 1; j < array.length; j++) {
			if (array[j] < array[index]) {
				index = j;
			}
		}
		 
		int temp = array[index];
		array[index] = array[0];
		array[0] = temp;
	}

This is a sort of result is 1,3,5,8,2,9,4,6,7,
Here Insert Picture Description
the code is so.

 public static void selectSort(int[] array){
	        for(int i=0;i<array.length-1;i++) {
	            int index = i;
	            for (int j = i+1; j < array.length; j++) {
	                if (array[j] < array[index]) {
	                    index = j;
	                }
	            }
	            //{1,2,5,8,3,9,4,6,7};
	            if(index!=i) {//如果已经是最小的,就不需要交换
	                int temp = array[index];
	                array[index] = array[i];
	                array[i] = temp;
	            }
	        }
	    }

Guess you like

Origin blog.csdn.net/qczg_wxg/article/details/89405470