0

using System.Linq;

        int min = 1;
        int max = 100;
        int[] a = new int[10];
        Random randNum = new Random();
        for (int i = 0; i < a.Length; i++)
        {
            a[i] = randNum.Next(min, max);
        }
        int t;
        Console.WriteLine("array :");
        foreach (int aa in a)
            Console.Write(aa + " ");
        Console.Write("\n");
        Console.WriteLine("Chosen number");
        {... }
            a = a.OrderBy(e => Math.Abs(e, b));
        foreach (var b in a) Console.Write("{0} ", x);
        {
            int b = int.Parse(Console.ReadLine());


            {
                for (int p = 0; p <= a.Length - 2; p++)
                {
                    for (int i = 0; i <= a.Length - 2; i++)
                    {
                        if (a[i]> a[i + 1])
                        {
                            t = a[i + 1];
                            a[i + 1] = a[i];
                            a[i] = t;

                        }

                    }
                }
                Console.Write("\n");
                Console.WriteLine("\n" + "+ :");
                foreach (int aa in a)
                    Console.Write(aa + " ");
                Console.Write("\n");
            }

This is Code I tried to use. but what I want to do is choosing certain number and sort nearest to farthest from the chosen number. For example there are 7, 5, 3, 2, 9 in array, and let's say I choose 5 from the array. Then the result should come out 5 7 3(order doesn't matter)2 9. Help me plz I am very noob TnT

장재현
  • 31
  • 1
  • 5

3 Answers3

1

Change condition (a[i] > a[i + 1]) to compare difference between (a[i] and b) and (a[i+1] and b)

Backs
  • 24,430
  • 5
  • 58
  • 85
0

You want to sort the array by the distance between the array element and the chosen number. Assuming that a is an array of numbers and b is the chosen number,

then replace for (;;) { ... } with this

using System.Linq;

a = a.OrderBy(e => (int) Math.Abs(e - b)).ToArray();
foreach (var x in a) Console.Write("{0} ", x);
Richard Schneider
  • 34,944
  • 9
  • 57
  • 73
0

Well, it took me some tome to understand what kind of sort you wanted, and then I've looked at your code and it seems to me you got your self into an over complicated method, so I've decided I'm not going to try and help you with your own code but write the entire sort from scratch.

I ended up with a method that uses an auxiliary array but only use a single loop to get the order done:

void StrangeSort(int[] arr, int num)
{
    var index = Array.IndexOf(arr, num);
    if(index > -1)
    {
        var copy = new int[arr.Length];
        int copyIndex = 0, 
            down = index-1, 
            up = index+1;
        copy[copyIndex] = arr[index];
        while(down > -1 || up < arr.Length)
        {
            if(down > -1)
            {
                copyIndex++;
                copy[copyIndex] = arr[down];
                down--;
            }
            if(up < arr.Length)
            {
                copyIndex++;
                copy[copyIndex] = arr[up];
                up++;
            }
        }
        copy.CopyTo(arr, 0);
    }
}

You can see a live demo on rextester.

And of course, there's always linq:

var index = Array.IndexOf(arr, num); // arr being your array, num being the value selected

arr = arr.Select((v, i) => new {Value = v, Index = i})
        .OrderBy(e => Math.Abs(index - e.Index))
        .Select(e => e.Value)
        .ToArray();
Zohar Peled
  • 79,642
  • 10
  • 69
  • 121