"Data Structure"--sorting algorithm--1.1, direct insertion sort

Source: Internet
Author: User
Tags comparable

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

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.