Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- public class InsertionSortTest
- {
- public static void Main(string[] args)
- {
- int []arr= {8,2,9,1,5,38,11,88,5};
- sortArray(arr);
- for (int i = 0; i < arr.Length; i++)
- {
- Console.WriteLine(arr[i]);
- }
- }
- static void sortArray(int[] arr)
- {
- int key; //3chan a7ot feh kemet element bt3y ely h3mlo sort
- for (int j = 1; j <arr.Length; j++)
- { //bfard en el arr[0] aslun sorted fa bbd2 btany element
- key = arr[j];
- int i = j - 1; //3la tool btshwar 3la element ely ablo 3cahn t3ml mokrnat bly abl kollhm
- //low i akbr min zero y3ny mwsltsh ll a5r el array wl arr[i] akbr mn lkey ly m3ya b5ly ely array of i yt7rk lodam
- //howa keda hms7 el value bt3t el j bass ana asln wa5d mnha copy fl key 3cahn h7toha fe mknha lsa7 b3deen
- while (i>= 0 && arr[i]>key)
- {
- arr[i + 1] = arr[i]; // by3ml shift
- i--; //b3d keda ashof element ely ablo l7ad ma awsl lnhyt al array
- }
- arr[i + 1] = key; //la7d ya b2a feh mkan fady ll element ly sorted y7t feh
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement