Algorithm weekly log (1) insert and sort directly

Source: Internet
Author: User
Tags visual studio 2010

AlgorithmIt plays an important role in the field of computer science. The design of a set of algorithms directly determines the success or failure of a project. In college education, the teacher taught us to use data structures and algorithms (now we need to add the architecture), but I have never learned these two basic courses. :) I plan to take a weekly note, carefully study an algorithm every week and use C #, Java, and MATLAB to study and study it with interested friends. In addition to common algorithm classification, I will pay special attention to numerical algorithms because I am very interested in mathematics. I am not good at C ++, so I do not provide C ++ algorithm implementation. I have a lot of information about C ++. If you are interested, you can find more information.

Download Sample Code

Test environment:

Windows Server 2008 r2 datacenter

C #: Visual Studio 2010 ultimate RC (. NET Framework 4.0 RC x64)

Java: Eclipse SDK 3.5.1 x64 (JDK 1.6 Update 18x64)

MATLAB: MATLAB r2009b x64

PS (Visual Studio 2010 official version is coming soon and will be updated later; MATLAB r2010a has been released, is being tested, and will be updated soon)

 

Insert sort directly

Basic Idea: each time a record to be sorted is inserted into the appropriate position of the previously sorted record according to its keyword size until all records are inserted.

Direct insertion of sorting refers to dividing a sequence into two parts. The first part is sorted, which is called "ordered area" in mathematics, and then from the second part ("unordered area ") insert the first record into the ordered area so that the inserted ordered area is still ordered. Repeat the preceding operation to reduce one record for each unordered partition until the number of records is zero.

Complexity: the time complexity is O (n ^ 2), and the space complexity is O (1 ).

It can be seen that N ^ 2 is still very large. When n is large, it is not suitable to use direct insertion sorting. The number of Recommendation records is n <20.

 

C # language description:

Code

Using System;
Using System. Collections. Generic;
Using System. LINQ;
Using System. text;

Namespace Insertsort
{
Class Insertsortdemo
{
Private   Static   Int [] Fxinsertsort ( Int [] ARR)
{
Int I, j, arrtmp;
For (I =   1 ; I < Arr. length; I ++ )
{
J = I;
Arrtmp = Arr [I];
While (J >   0   && Arr [J -   1 ] > Arrtmp)
{
Arr [J] = Arr [J -   1 ];
J -- ;
}
Arr [J] = Arrtmp;
}
Return Arr;
}

Static   Void Main ( String [] ARGs)
{
Int [] Arr = Insertsortdemo. fxinsertsort ( New   Int [] { 1 , 4 , 2 , 6 , 5 });
Foreach ( Int I In ARR)
{
Console. Write (I +   " , " );
}
Console. Readline ();
}
}
}

 

Java language description:

Code

Package Brooks. cnblogs. arithmetic. Sort;

Public ClassInsertsortdemo {

/**
* @ Param ARGs
*/
Private   Static   Int [] Fxinsertsort ( Int [] ARR ){
Int I, j, arrtmp;
For (I =   1 ; I < Arr. length; I ++ ){
J = I;
Arrtmp = Arr [I];
While (J >   0   && Arr [J -   1 ] > Arrtmp ){
Arr [J] = Arr [J -   1 ];
J -- ;
}
Arr [J] = Arrtmp;
}
Return Arr;
}

Public   Static   Void Main (string [] ARGs ){
// Todo auto-generated method stub
Int [] Arr = Insertsortdemo. fxinsertsort ( New   Int [] { 1 , 4 , 2 , 6 , 5 });

for ( int I = 0 ; I arr. length; I ++ ) {
system. out. print (ARR [I] + " , " );
}< BR >}

 

MATLAB language description:

Code

% Define an array
Arr = [ 1   4   2   6   5 ] ;
% Array index in MATLAB starts from 1
For I =   2 : Length ( Arr )
J = I ;
Arrtmp = Arr ( I );
While J >   1 && ( Arr ( J- 1 )   > Arrtmp )
Arr ( J )   = Arr ( J- 1 );
J = J- 1 ;
End
Arr ( J )   = Arrtmp ;
End
% Output the sorted result
Disp ( Arr );

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.