On Java data structure and algorithm

Source: Internet
Author: User
Tags array length

Today's sudden look at the bottom of the collection found a lot of algorithms and data structures. Once again, compare and summarize.

Data structure classification: linear structure and nonlinear structure

Question one:

What is linear and nonlinear;

My personal understanding is that the linear structure in the data structure refers to the data structures in which there is a "one-to-one" linear relationship between the elements.

Linear structures include: arrays, lists, queues, stacks;

Nonlinear structures include: trees, graphs, tables;

Detailed

I. LINEAR structure

1. Arrays

Features: We all know that the elements in the array are stored continuously in memory, can be quickly accessed based on the subscript, so the query is fast, however, when inserting and deleting, it is necessary to move space to the element, relatively slow.

Array usage scenarios: frequently queried, rarely added and deleted cases.

2. Linked list

Features: Elements can be in a discontinuous memory, is indexed to the data link, when the query elements need to start from scratch, so the efficiency is low, but add and delete only need to modify the index can be

Usage scenarios: Fewer queries, frequent insertions or deletions

3. Queues

Features: Advanced First Out,

Usage Scenario: Multithreading blocking queue management is useful

4. Stack

Features: Advanced after out, like a box,

Usage scenarios: Implementing Recursion and expressions

5. The difference between an array and a linked list

Array contiguous, linked list discontinuous (in the form of data storage)

Array memory static allocation, linked list dynamic allocation

Array Query complexity 0 (1), List query complexity O (n)

Array additions or deletions, complexity O (n), linked list add delete, complexity O (1)

The array allocates memory from the stack. The linked list allocates memory from the heap.

Two. Algorithm classification:

1) Insert sort (direct insert sort, hill Sort)

2) Swap sort (bubble sort, quick sort)

3) Select sort (Direct select sort, heap sort)

4) Merge sort

5) Allocation sort (cardinal sort)

Maximum required auxiliary space: merge sort

Minimum required secondary space: heap Ordering

Fastest Speed: Fast sorting

Unstable: Quick sort, hill sort, heap sort.

1. Direct Insert Sort

(1) Basic idea: In the set of numbers to be sorted, assume that the number of front (n-1) [n>=2] is already a row

In a good order, now you want to insert the nth number into the ordinal number in front, so that the n number

It's also in the right order. This cycle is repeated until all the rows are in order.

/*** Insert Sort method * *@paramdatas*/       Public Static int[] Sortinsert (int[] datas) {           for(inti = 1; i < datas.length; i++) {              intj = I-1; Algorithmutil.temp=Datas[i];  for(; J >= 0 && Algorithmutil.temp < datas[j]; j--) {datas[j+ 1] =Datas[j]; } datas[j+ 1] =algorithmutil.temp; }          returndatas; }  

2. Simple selection of sorting

(1) Basic idea: In the group of numbers to be sorted, choose the smallest one and the number of the first position to exchange;

Then in the rest of the number to find the smallest and second position of the number of exchanges, so loop to the penultimate number and the last

Number of comparisons.

 /*** Select Sort * *@return      */       Public Static int[] Sortselect (int[] datas) {           for(inti = 0; i < datas.length; i++) {              intindex =i;  for(intj = i + 1; J < Datas.length; J + +) {                  if(Datas[j] <Datas[index]) index=J; }              if(I! =index)          Algorithmutil.swap (datas, I, index); }          returndatas; }  

3. Bubble sort

(1) Basic idea: In the group of numbers to be sorted, the total number in the range that is not yet well sequenced, top-down

The adjacent two numbers are compared and adjusted sequentially, allowing the larger number to sink and the smaller one to go up. That is, whenever two adjacent

The comparison of the numbers shows that they are reversed when they are ordered in the opposite order.

 /*** Bubble Sort * *@return      */       Public Static int[] Sortbubble (int[] datas) {           for(inti = 0; i < datas.length-1; i++) {               for(intj = 0; J < datas.length-1-I; J + +) {                  if(Datas[j] > datas[j + 1]) Algorithmutil.swap (Datas, J, J+ 1); }          }          returndatas; }  

4. Quick Sort

(1) Basic idea: Select a datum element, usually select the first element or the last element, through a scan,

Divides the pending sequence into two parts, one smaller than the Datum element, and one part greater than or equal to the datum element, at which point the datum element

Arrange the correct position after the order, and then use the same method recursively to sort the two parts of the division.

  /*** Quick sort; Split array * *@paramdatas*/       Public Static intQuickpartition (int[] datas,intLeftintRight ) {          intPivot =Datas[left];  while(Left <Right ) {               while(Left < right && Datas[right] >=pivot)--Right ; Datas[left]= Datas[right];//move the element smaller than the pivot to the lower end, at which point the right bit is equal to NULL, waiting for the lower-than-pivotkey number to fill up             while(Left < right && Datas[left] <=pivot)++Left ; Datas[right]= Datas[left];//move the element larger than the pivot to the high end, at which point the left bit is equal to empty, waiting for a higher number to be smaller than the PivotKey .} Datas[left]= pivot;//when left = = right, complete a quick sequence, the left bit is equivalent to empty, waiting for PivotKey to fill up        returnLeft ; }        /*** Quick sort; recursive return array * *@paramdatas*/       Public Static int[] Sortquick (int[] datas,intLeftintRight ) {          if(Left <Right ) {              intdata =quickpartition (datas, left, right); Sortquick (Datas, left, data-1); Sortquick (datas, Data+ 1, right); }          returndatas; }  

1. Bubbling algorithm, 2. Select algorithm, 3. Fast algorithm. 4. Insert algorithm, 5. Hill algorithm, 6. Heap algorithm

 Public classAlgorithmutil { Public Static intTemp,index = 0; /*** Temporary Value exchange * *@paramdatas * Array *@paramI *@paramJ*/   Public Static voidSwapint[] datas,intIintj) {Temp=Datas[i]; Datas[i]=Datas[j]; DATAS[J]=temp; }  /*** Extended Array length * *@paramDatas *@paramValue *@return  */   Public Static int[] Expandarray (int[] datas,intvalue) {      if(Datas.length <=index) {          int[] Arrays =New int[Datas.length * 2]; System.arraycopy (Datas,0, arrays, 0, datas.length); Datas=arrays; } Datas[index]=value; Index++; returndatas; }  }  

Now remember. Will bubble. Insert. Choose. Fast. Algorithm.

On Java data structure and algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.