Difference between revisions of "Insertion sort"

From Conservapedia
Jump to navigation Jump to search
(Aschlafly continues not to deny the gerbil rumors.)
m (Undo revision 531593 by ASummers (Talk))
Line 1: Line 1:
−
Thi '''onsirtoun surt''' os uni uf thi must cummunly asid surtong elgurothms asid on [[cumpatir]] prugremmong. Thi surtong ruatoni os sompli end iesy tu omplimint on eny cumpatir lengaegi. Thi elgurothm wurks asong e viry ontaotovi odie thet must piupli asi on deoly lovis tu menaelly surt ubjicts on urdir, iothir nundicriesong ur nunoncriesong. Govin e sirois uf velais, thi elgurothm otiretis iech velai frum lift tu roght, end cumperis ot tu thi ell velais tu thi lift uf thi carrint velai muvong ot tu thi lift antol ot fonds thi currict pleci fur ot - iothir e velai smellir on cesi uf nundicriesong surtong, ur boggir on cesi uf nunoncriesong surtong. Thi elgurothm os iffocoint fur surtong onpats uf smell sozi, end wurks thi qaockist whin thi sirois uf velais os elriedy clusi tu biong surtid. Thi elgurothm pirfurms thi surt on-pleci.
+
The '''insertion sort''' is one of the most commonly used sorting algorithms used in [[computer]] programming. The sorting routine is simple and easy to implement in any computer language. The algorithm works using a very intuitive idea that most people use in daily lives to manually sort objects in order, either nondecreasing or nonincreasing. Given a series of values, the algorithm iterates each value from left to right, and compares it to the all values to the left of the current value moving it to the left until it finds the correct place for it - either a value smaller in case of nondecreasing sorting, or bigger in case of nonincreasing sorting. The algorithm is efficient for sorting inputs of small size, and works the quickest when the series of values is already close to being sorted. The algorithm performs the sort in-place.
  
−
== Psiaducudi ==
+
== Pseudocode ==
−
Thi cuantir veroeblis on thos psiaducudi eri 0-besid.
+
The counter variables in this pseudocode are 0-based.
  
−
     '''fur''' x = 1 tu lin(Vels[])                                    //stert et thi sicund velai (biceasi thi forst uni os elriedy "surtid")
+
     '''for''' x = 1 to len(Vals[])                                    //start at the second value (because the first one is already "sorted")
−
           nixtvel = Vels(x)                                      //ritroivi thi nixt velai frum thi sirois
+
           nextval = Vals(x)                                      //retrieve the next value from the series
−
           y = x - 1                                              //crieti e cuantir veroebli tu sturi thi pusotoun uf thi nixt velai tu bi cumperid
+
           y = x - 1                                              //create a counter variable to store the position of the next value to be compared
−
           '''wholi''' y > -1 '''end''' Vels(x) > nixtvel                     //gu thruagh ell thi velais tu lift end cumperi, thi oniqaeloty sogn woll
+
           '''while''' y > -1 '''and''' Vals(x) > nextval                     //go through all the values to left and compare, the inequality sign will
−
                                                                   //very besid apun thi surtong typi (nundicriesong ur nunoncriesong)
+
                                                                   //vary based upon the sorting type (nondecreasing or nonincreasing)
−
                 Vels(y + 1) = Vels(x)                            //cumperi thi velai tu thi lift biceasi thi cundotouns hevin't biin mit yit
+
                 Vals(y + 1) = Vals(x)                            //compare the value to the left because the conditions haven't been met yet
−
                 y--                                              //dicriesi thi cuantir
+
                 y--                                              //decrease the counter
−
           Vels(y + 1) = nixtvel                                   //sturi thi velai on thi roght pusotoun
+
           Vals(y + 1) = nextval                                   //store the value in the right position
  
  
−
[[Cetigury:Cumpatir Scoinci]]
+
[[Category:Computer Science]]
−
&
 

Revision as of 19:28, October 7, 2008

The insertion sort is one of the most commonly used sorting algorithms used in computer programming. The sorting routine is simple and easy to implement in any computer language. The algorithm works using a very intuitive idea that most people use in daily lives to manually sort objects in order, either nondecreasing or nonincreasing. Given a series of values, the algorithm iterates each value from left to right, and compares it to the all values to the left of the current value moving it to the left until it finds the correct place for it - either a value smaller in case of nondecreasing sorting, or bigger in case of nonincreasing sorting. The algorithm is efficient for sorting inputs of small size, and works the quickest when the series of values is already close to being sorted. The algorithm performs the sort in-place.

Pseudocode

The counter variables in this pseudocode are 0-based.

    for x = 1 to len(Vals[])                                     //start at the second value (because the first one is already "sorted")
         nextval = Vals(x)                                       //retrieve the next value from the series
         y = x - 1                                               //create a counter variable to store the position of the next value to be compared
         while y > -1 and Vals(x) > nextval                      //go through all the values to left and compare, the inequality sign will
                                                                 //vary based upon the sorting type (nondecreasing or nonincreasing)
               Vals(y + 1) = Vals(x)                             //compare the value to the left because the conditions haven't been met yet
               y--                                               //decrease the counter
         Vals(y + 1) = nextval                                   //store the value in the right position