1

I created the pair class and array class, but I'm lost on how to implement the quicksort algorithm. I want to do it if ints are same then I should sort by double. I was able to implement quicksort with one value per index of array, but with this I just can't find any resources.

Maybe you guys have some resources or maybe you had the same problem?

By the way I'm trying to implement it with c#.

This is my pair class:

class Pair
{
    public int integer = 0;
    public double doubl = 0.0;

    public Pair(int integer, double doubl)
    {
        this.integer = integer;
        this.doubl = doubl;
    }

    public Pair()
    {

    }
    public int Integer() { return integer; }
    public double Doubl() { return doubl; }
}

And my data array class

class MyDataArray : DataArray
{
    Pair[] data;
    int operations = 0;
    public MyDataArray(int n, int seed)
    {
        data = new Pair[n];
        Random rand = new Random(seed);
        for (int i = 0; i < n; i++)
        {
            data[i] = new Pair(rand.Next(1,100), rand.NextDouble());
        }

    }

    public override int integer(int index)
    {
        return data[index].integer;

    }

    public override double doubl(int index)
    {
        return data[index].doubl;
    }

    public override void Swap(int i, int j)
    {
        Pair temp = data[i]; // c3    1
        data[i] = data[j]; // c3    1
        data[j] = temp; // c3   1

    }
Haidrex
  • 25
  • 5
  • Just compare the ints and if they’re the same then compare doubles, as you said. Otherwise it’s exactly the same as sorting by anything. – Sami Kuhmonen Mar 30 '20 at 19:01
  • You need to decide what makes one instance greater than another (do you sort by ints first or doubles first). You need to define what equality means (remember, equality for floating points is tricky). Then you should implement the appropriate interfaces (example `IComparable`) and use it for your comparison. There are rules that must be respected (for example, if A>B and C>A, then C>B, but not just that). – Flydog57 Mar 30 '20 at 19:05
  • Side note: using public field is not a recommended approach. Also using method to what looks like properties (`Integer()` and `Doubl()`) is very confusing. `public Integer {get;set;}` would be shorter and more conventional. – Alexei Levenkov Mar 30 '20 at 19:28

3 Answers3

1

Your Pair class could implement IComparable<T>, and your quick sort algorithm could be implemented using the CompareTo method.

The IComparable<T> interface:

Defines a generalized comparison method that a value type or class implements to create a type-specific comparison method for ordering or sorting its instances.

You can see the documentation on the CompareTo method to see what the return values mean.

public class Pair : IComparable<Pair>
{
    public int integer = 0;
    public double doubl = 0.0;

    public Pair(int integer, double doubl)
    {
        this.integer = integer;
        this.doubl = doubl;
    }

    public Pair()
    {

    }

    public int CompareTo(Pair other)
    {
       if (other == null)
       {
           return 1;
       }
       int result = integer.CompareTo(other.integer);
       if (result == 0)
       {
           result = doubl.CompareTo(other.doubl);
       }
       return result;
    }

    public int Integer() { return integer; }
    public double Doubl() { return doubl; }
}

If you prefer to use the comparison operators, you can implement them in terms of the CompareTo method. The documentation I liked has examples on how to do that.

Joshua Robinson
  • 3,399
  • 7
  • 22
0
//sort for integer
var SortedIntegerList = data.OrderBy(x=>x.integer);
//sort for double
var SortedDoubleList = data.OrderBy(x=>x.doubl);
0

OrderBy for objects uses Quicksort - What Sorting Algorithm Is Used By LINQ "OrderBy"? - so you can use that.

To avoid creating IComparer<Pair> interface you can construct it using Comparer<T>.Create from just comparison delegate:

var sorted = source.OrderBy(x => x, Comparer<Pair>.Create(
      (p1, p2) => p1.Integer() - p2.Integer() != 0 ?
         p1.Integer() - p2.Integer() :
         Math.Sign(p1.Doubl() - p2.Doubl()))).ToList();
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179