Insertion Sorting c#

前端 未结 3 772
暗喜
暗喜 2021-01-29 12:10

Can you guys please help me with basic insertion sorting in C#. I have a list of names and city of residence in a array and need to sort this array by comparing the city of resi

3条回答
  •  花落未央
    2021-01-29 12:44

    int[] newarr = {2,1,5,3,7,6};
    int a, b;
    for (int i = 1; i < newarr.Length; i++)
    {
        a = newarr[i];
        b = i - 1;
        while(b>=0 && newarr[b]>a)
        {
            newarr[b+1] = newarr[b];
            b=b-1;
        }
        newarr[b+1] = a;
    }
    

提交回复
热议问题