Many of the insertion algorithms, whether in the internal training, a variety of written interviews are very useful. Next, the various sorting algorithms will be practiced in succession:
The main sections are divided into the following parts (other later learning supplements):
First, the insertion class sort: 1, the direct insertion sort, 2, binary inserts the sort; 3, the hill shell sorts;
Second, the Exchange class sort: 1, bubble sort; 2, quick sort;
Third, the choice of class sort: 1, Simple choice, 2, heap sorting;
I use java--to start!
First recommended 1, Wikipedia, "Sorting algorithm" entry, illustrated, very image! 2, study blog "Wikipedia on the algorithm and data structure link is very powerful", a lot of information, save learning!
"Data Structure"--sorting algorithm--1.1, direct insertion sort
First, on the wiki map:
Figure I, insert sort example
Classification |
Sorting algorithms |
Data |
Array |
Worst-of-time complexity |
|
Optimal time complexity |
|
Average Time complexity |
|
Worst-of-space complexity |
In total, a secondary space is required
|
Ii. Descriptionvery simple, similar to playing poker when small to large row. Every time a person sends a card, take it up and tidy it up. Pick up the new card, the first thing is to look at the size of the card, and then from the hand to organize the cards from the big to small (or from the beginning to the big) in the direction of inquiry. When a position is found to satisfy one side greater than or equal to the new card, and the other side is less than or equal to the new card, the new card is inserted into the position. Repeat, the final card is sorted!
Third, Java program
public class Insertion {public static void Insertionsort (comparable []data) {for (int index = 1; index < data. Length index++) { Comparable key = Data[index]; int position = index; Shift larger values to the right while (position > 0 && data[position-1].compareto (key) > 0) { D Ata[position] = data[position-1]; position--; } Data[position] = key; } } public static void Main (String []args) { comparable []c = {4, 9, 1, 5, 2}; Insertionsort (c); for (int i = 0; i < c.length; i++) System.out.println ("Insert sort:" + c[i]); } }
"Data Structure"--sorting algorithm--1.1, direct insertion sort