Simon
Simon

Reputation: 381

Sorting a list manually

So basically the user is able to fill a number, that number gets added to a list and once the user pressed a button the list should be sorted without using the .Sort(); function

    public int[] nummers;
    public ObservableCollection<int> alleNummers = new ObservableCollection<int>();
    public MainWindow()
    {
        InitializeComponent();
    }

    private void button_Click(object sender, RoutedEventArgs e)
    {
        alleNummers.Add(int.Parse(nummerBox.Text));
        listBox.ItemsSource = alleNummers;
    }

    private void sortBtn_Click(object sender, RoutedEventArgs e)
    {
        foreach (int i in alleNummers)
        {
            //Sort the int's from small to big
        }
        nummers = alleNummers.ToArray();
        listBox.ItemsSource = nummers;
    }

I think, I need to make use of a foreach loop but wouldn't know what to type. This is a small homework assignment btw.

Upvotes: 0

Views: 5334

Answers (4)

Sid
Sid

Reputation: 317

Try this! this approach will short the list by ascending order.

        List<int> lst = new List<int>() { 2, 3, 1, 0, 5 };

        int j=0;

        while (j < lst.Count)
        {
            for (int i = lst.Count - 1; i >= 0; i--)
            {
                if ((i - 1) >= 0 && lst[i] < lst[i - 1])
                {
                    int temp = lst[i];
                    lst[i] = lst[i - 1];
                    lst[i - 1] = temp;
                }

            }
            j++;
        }

Upvotes: 0

Vikas Bansal
Vikas Bansal

Reputation: 11730

Here are some algorithms that you can follow to sort your list programmatically Sorting Algorithms with Code and great explanation

Bubble Sort

 public static void IntArrayBubbleSort (int[] data)
  {
     int i, j;
     int N = data.Length;

     for (j=N-1; j>0; j--) {
        for (i=0; i<j; i++) {
           if (data [i] > data [i + 1])
              exchange (data, i, i + 1);
        }
     }
  }

Selection Sort

 public static int IntArrayMin (int[] data, int start)
      {
         int minPos = start; 
         for (int pos=start+1; pos < data.Length; pos++)
            if (data [pos] < data [minPos])
               minPos = pos;
         return minPos; 
      }

      public static void IntArraySelectionSort (int[] data)
      {
         int i;
         int N = data.Length;

         for (i=0; i < N-1; i++) {
            int k = IntArrayMin (data, i);
            if (i != k)
               exchange (data, i, k);
         }
      }

Insertion Sort

 public static void IntArrayInsertionSort (int[] data)
      {
         int i, j;
         int N = data.Length;

         for (j=1; j<N; j++) {
            for (i=j; i>0 && data[i] < data[i-1]; i--) {
               exchange (data, i, i - 1);
            }
         }
      }

Shell Sort

   static int[] GenerateIntervals (int n)
  {
     if (n < 2) {  // no sorting will be needed
        return new int[0];
     }
     int t = Math.Max (1, (int)Math.Log (n, 3) - 1);
     int[] intervals = new int[t];       
     intervals [0] = 1;
     for (int i=1; i < t; i++)
        intervals [i] = 3 * intervals [i - 1] + 1;
     return intervals;
  }

  public static void IntArrayShellSortBetter (int[] data)
  {
     int[] intervals = GenerateIntervals (data.Length);
     IntArrayShellSort (data, intervals);
  }

Quicksort a.k.a. Partition Sort

  public static void IntArrayQuickSort (int[] data, int l, int r)
      {
         int i, j;
         int x;

         i = l;
         j = r;

         x = data [(l + r) / 2]; /* find pivot item */
         while (true) {
            while (data[i] < x)
               i++;
            while (x < data[j])
               j--;
            if (i <= j) {
               exchange (data, i, j);
               i++;
               j--;
            }
            if (i > j)
               break;
         }
         if (l < j)
            IntArrayQuickSort (data, l, j);
         if (i < r)
            IntArrayQuickSort (data, i, r);
      }

      public static void IntArrayQuickSort (int[] data)
      {
         IntArrayQuickSort (data, 0, data.Length - 1);
      }

Random Data Generation

 public static void IntArrayGenerate (int[] data, int randomSeed)
      {
         Random r = new Random (randomSeed);
         for (int i=0; i < data.Length; i++)
            data [i] = r.Next ();
      }

Upvotes: 5

Zabi
Zabi

Reputation: 1369

I would like to offer up this guide as a solution to your question

Sorting algorithms represent foundational knowledge that every computer scientist and IT professional should at least know at a basic level. And it turns out to be a great way of learning about why arrays are important well beyond mathematics.

In this section, we’re going to take a look at a number of well-known sorting algorithms with the hope of sensitizing you to the notion of performance–a topic that is covered in greater detail in courses such as algorithms and data structures

Introduction to Computer Science in C# 10.4. Sorting Algorithms

it contains many examples, including Bubble Sort:

public static void IntArrayBubbleSort (int[] data)
  {
     int i, j;
     int N = data.Length;

     for (j=N-1; j>0; j--) {
        for (i=0; i<j; i++) {
           if (data [i] > data [i + 1])
              exchange (data, i, i + 1);
        }
     }
  }

Upvotes: 1

fubo
fubo

Reputation: 45947

here is a implementation of the BubbleSort algorithm. This is one of the slowest but also one of the "easy to understand" sorting algorithms.

for (int i = (alleNummers.Count - 1); i >= 0; i--)
{
    for (int j = 1; j <= i; j++)
    {
        if (alleNummers[j - 1] > alleNummers[j])
        {
            var temp = alleNummers[j - 1];
            alleNummers[j - 1] = alleNummers[j];
            alleNummers[j] = temp;
        }
    }
}

if you want to use another one, i mentioned some in one of my recent answers: I want an efficient sorting algorithm to sort an array

Upvotes: 0

Related Questions