Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. Math Namespace?

Math Namespace?

Scheduled Pinned Locked Moved .NET (Core and Framework)
question
10 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    igor1960
    wrote on last edited by
    #1

    Math Namespace? Give me "qsort" please!...

    J L J 3 Replies Last reply
    0
    • I igor1960

      Math Namespace? Give me "qsort" please!...

      J Offline
      J Offline
      J Dunlap
      wrote on last edited by
      #2

      igor1960 wrote: Math Namespace? System.Math - it's a class, not a namespace. igor1960 wrote: "qsort" :confused:

      "Blessed are the peacemakers, for they shall be called sons of God." - Jesus
      "You must be the change you wish to see in the world." - Mahatma Gandhi

      1 Reply Last reply
      0
      • I igor1960

        Math Namespace? Give me "qsort" please!...

        L Offline
        L Offline
        leppie
        wrote on last edited by
        #3

        quicksort for you, strait from Rotor.

        private class SorterObjectArray
        {
        private Object[] keys;
        private Object[] items;
        private IComparer comparer;

        public SorterObjectArray(Object\[\] keys, Object\[\] items, IComparer comparer) {
            if (comparer == null) comparer = Comparer.Default;
            this.keys = keys;
            this.items = items;
            this.comparer = comparer;
        }
        
        public virtual void QuickSort(int left, int right) {
            // Can use the much faster jit helpers for array access.
            do {
                int i = left;
                int j = right;
                Object x = keys\[(i + j) >> 1\];
                do {
                    // Add a try block here to detect IComparers (or their
                    // underlying IComparables, etc) that are bogus.
                    try {
                        while (comparer.Compare(keys\[i\], x) < 0) i++;
                        while (comparer.Compare(x, keys\[j\]) < 0) j--;
                    }
                    catch (IndexOutOfRangeException) {
                        throw new ArgumentException(String.Format(Environment.GetResourceString("Arg\_BogusIComparer"), x, x.GetType().Name, comparer));
                    }
                    catch (Exception e) {
                        throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation\_IComparerFailed"), e);
                    }
                    BCLDebug.Assert(i>=left && j<=right, "(i>=left && j<=right)  Sort failed - Is your IComparer bogus?");
                    if (i > j) break;
                    if (i < j) {
                        Object key = keys\[i\];
                        keys\[i\] = keys\[j\];
                        keys\[j\] = key;
                        if (items != null) {
                            Object item = items\[i\];
                            items\[i\] = items\[j\];
                            items\[j\] = item;
                        }
                    }
                    i++;
                    j--;
                } while (i <= j);
                if (j - left <= right - i) {
                    if (left < j) QuickSort(left, j);
                    left = i;
                }
                else {
                    if (i < right) QuickSort(i, right);
                    right = j;
                }
            } while (left < right);
        }
        

        }

        leppie::AllocCPArticle(

        I 1 Reply Last reply
        0
        • L leppie

          quicksort for you, strait from Rotor.

          private class SorterObjectArray
          {
          private Object[] keys;
          private Object[] items;
          private IComparer comparer;

          public SorterObjectArray(Object\[\] keys, Object\[\] items, IComparer comparer) {
              if (comparer == null) comparer = Comparer.Default;
              this.keys = keys;
              this.items = items;
              this.comparer = comparer;
          }
          
          public virtual void QuickSort(int left, int right) {
              // Can use the much faster jit helpers for array access.
              do {
                  int i = left;
                  int j = right;
                  Object x = keys\[(i + j) >> 1\];
                  do {
                      // Add a try block here to detect IComparers (or their
                      // underlying IComparables, etc) that are bogus.
                      try {
                          while (comparer.Compare(keys\[i\], x) < 0) i++;
                          while (comparer.Compare(x, keys\[j\]) < 0) j--;
                      }
                      catch (IndexOutOfRangeException) {
                          throw new ArgumentException(String.Format(Environment.GetResourceString("Arg\_BogusIComparer"), x, x.GetType().Name, comparer));
                      }
                      catch (Exception e) {
                          throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation\_IComparerFailed"), e);
                      }
                      BCLDebug.Assert(i>=left && j<=right, "(i>=left && j<=right)  Sort failed - Is your IComparer bogus?");
                      if (i > j) break;
                      if (i < j) {
                          Object key = keys\[i\];
                          keys\[i\] = keys\[j\];
                          keys\[j\] = key;
                          if (items != null) {
                              Object item = items\[i\];
                              items\[i\] = items\[j\];
                              items\[j\] = item;
                          }
                      }
                      i++;
                      j--;
                  } while (i <= j);
                  if (j - left <= right - i) {
                      if (left < j) QuickSort(left, j);
                      left = i;
                  }
                  else {
                      if (i < right) QuickSort(i, right);
                      right = j;
                  }
              } while (left < right);
          }
          

          }

          leppie::AllocCPArticle(

          I Offline
          I Offline
          igor1960
          wrote on last edited by
          #4

          Thanx. Looks like real qsort. Not as optimized as CRT, but still good enough. Thanx again.

          1 Reply Last reply
          0
          • I igor1960

            Math Namespace? Give me "qsort" please!...

            J Offline
            J Offline
            James T Johnson
            wrote on last edited by
            #5

            Any reason you can't use the Array.Sort static method? I think that is the method that leppie gave the code from. James "I despise the city and much prefer being where a traffic jam means a line-up at McDonald's" Me when telling a friend why I wouldn't want to live with him

            L I 2 Replies Last reply
            0
            • J James T Johnson

              Any reason you can't use the Array.Sort static method? I think that is the method that leppie gave the code from. James "I despise the city and much prefer being where a traffic jam means a line-up at McDonald's" Me when telling a friend why I wouldn't want to live with him

              L Offline
              L Offline
              leppie
              wrote on last edited by
              #6

              James T. Johnson wrote: I think that is the method that leppie gave the code from. Correct :) You are one of the few people that actually types "leppie" not "Leppie" :laugh: I wonder why some people capitalize the 'l'? leppie::AllocCPArticle(Generic DFA State Machine for .NET);

              N 1 Reply Last reply
              0
              • J James T Johnson

                Any reason you can't use the Array.Sort static method? I think that is the method that leppie gave the code from. James "I despise the city and much prefer being where a traffic jam means a line-up at McDonald's" Me when telling a friend why I wouldn't want to live with him

                I Offline
                I Offline
                igor1960
                wrote on last edited by
                #7

                Yes, I figured that out. Just couldn't find SorterObjectArray implementation in Reflector. Now it's ok. Thnx

                1 Reply Last reply
                0
                • L leppie

                  James T. Johnson wrote: I think that is the method that leppie gave the code from. Correct :) You are one of the few people that actually types "leppie" not "Leppie" :laugh: I wonder why some people capitalize the 'l'? leppie::AllocCPArticle(Generic DFA State Machine for .NET);

                  N Offline
                  N Offline
                  Not Active
                  wrote on last edited by
                  #8

                  I'm sure they will spell it correctly on the plagarism award.

                  L 1 Reply Last reply
                  0
                  • N Not Active

                    I'm sure they will spell it correctly on the plagarism award.

                    L Offline
                    L Offline
                    leppie
                    wrote on last edited by
                    #9

                    Maybe you need glasses, or didnt you notice the "From Rotor" part? Perhaps invest in a dictionary. leppie::AllocCPArticle(Generic DFA State Machine for .NET);

                    N 1 Reply Last reply
                    0
                    • L leppie

                      Maybe you need glasses, or didnt you notice the "From Rotor" part? Perhaps invest in a dictionary. leppie::AllocCPArticle(Generic DFA State Machine for .NET);

                      N Offline
                      N Offline
                      Not Active
                      wrote on last edited by
                      #10

                      I do wear glasses thank you. Perhaps you could recommend an Optometrist or you could write a dictionary for me( a copy www.m-w.com would be fine) Please respond so it boosts your posted rate, after all quantity certainly beats quality. :rolleyes: Sorry I forgot about the clickety police www.m-w.com[^]

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups