Click here to Skip to main content
15,901,122 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hello Experts,

This may be very simple for you, but this is giving me a very hard time. I am trying to Sort An LIST of strings. i found varios examples how to sort an array of strings but none of how can i sort an list of strings. please help me as this may only take 5min for you but it already took me 2 days!The problem i am having is that: it is checking each while loop but never getting in. I think this might have something to do with the Icomparable as i noticed from some articles. No warning no errors :/ ??

Thanks for your help. Please find the code i am using:
C#
**********************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace myDemo
{
    class Program
    {
        static void Main()
        {
            string[] unsorted = { "z", "e", "x", "c", "m", "q", "a" };

            List<string> myList = new List<string>();
            myList.AddRange(unsorted);

            for (int i = 0; i < myList.Count; i++)
            {
                Console.WriteLine(myList[i]);
            }

            Console.WriteLine("Sorted");

            List<string> Sorted = Sorting(myList, 0, myList.Count - 1);

            for (int i = 0; i < Sorted.Count; i++)
            {
                Console.WriteLine(Sorted[i]);
            }
            Console.ReadKey();
        }

        public static List<string> Sorting(List<string> Unsorted, int lowerLimit, int upperLimit)
        {
            if (Unsorted.Count <= 2)
            {
                return Unsorted;
            }

            //Creating Needed Variables
            int Pivot = Unsorted.Count / 2; //with return an integer not a decimal number, if input is integer
            int Lower = lowerLimit, Greater = upperLimit;

            while (Lower <= Greater)
            {
                while (Unsorted[Lower].CompareTo(Unsorted[Pivot]) < 0)
                {
                    Lower++;
                }
                while (Unsorted[Greater].CompareTo(Unsorted[Pivot]) > 0)
                {
                    Greater--;
                }

                if (Lower == Greater)
                {
                    //swap the elements
                    string temp = Unsorted[Lower];
                    Unsorted[Lower] = Unsorted[Greater];
                    Unsorted[Greater] = temp;

                    Lower++;
                    Greater--;
                }
            }

            if (lowerLimit < Greater)
            {
                Sorting(Unsorted, lowerLimit, Greater);
            }
            if (Lower < upperLimit)
            {
                Sorting(Unsorted, Lower, upperLimit);
            }

            return Unsorted;
        }
    }
}
Posted
Updated 26-Dec-12 12:31pm
v2
Comments
Sergey Alexandrovich Kryukov 26-Dec-12 18:32pm    
For proper formatting of code, use "pre" tags and escape HTML entities like '<', '>'. This time, I've done this for you, you can see how to do it if you click "Improve question".
—SA
Gilmore Sacco 26-Dec-12 18:43pm    
Thank you very much, this is only my first post since i am still a very basic, but i shall try to improve and show kindness as you did with me
Sergey Alexandrovich Kryukov 26-Dec-12 18:44pm    
You are very welcome. Not a problem at all.
—SA
Sergey Alexandrovich Kryukov 26-Dec-12 18:34pm    
You did not explain the problem. Besides, all an array can do, a list can do, too, so implementation of the algorithm is easier with a list, not harder.
—SA
Gilmore Sacco 26-Dec-12 18:40pm    
Hello sir, Thx for the quick reply,
As you most certainly noticed i am trying to implement the quick sort algorithm, My problem is that it it not solting since all the conditions of the while loop are failing, and therefore Greater & lower are not being manipulated, therefore the main whileloop condition is never sadisfied. Thx :)

1 solution

The algorithm is wrong from the very beginning, the signature, as the sort method should not depends on any initial indices. The input should get a list parameter and nothing else. In some implementations, you will need to create another function for recursive calls.

I don't know what algorithm description did you use, but there are the pseudo-codes for different versions of the algorithm, pretty simple:
http://en.wikipedia.org/wiki/Quick_sort[^].

Also, do you use the debugger during development? You should. The problem is pretty simple, you should better solve it to the very end. By yourself.

[EDIT]

I took the original code (still, it's not good enough, you should do what I mentioned about the signature), it worked out immediately after I changed it to use List. First, use System.Collections.Generic.List<IComparable>, as in original code. Change array declaration into list, add population of the list using AddRange and don't do anything else. Most importantly, do not return unsorted. The algorithm works on arrays, so it does not use any list features and does not create any arrays, so you should not create any lists. The algorithm works in-place. Make the return type void. Show the same list after sorting as the one you've shown before — it will be sorted.

—SA
 
Share this answer
 
v3
Comments
Gilmore Sacco 26-Dec-12 19:14pm    
Since i am trying to learn C# by myself i am using some codes found online and trying to manipulate them so that i can learn. i found this code at http://snipd.net/quicksort-in-c and it works well on visual studio. as you said yourself a list is better then an list, but still it is not working. If you have the time to check out the link, it shows that the only defference is that it is using a Icontrolable array
Sergey Alexandrovich Kryukov 26-Dec-12 20:06pm    
Resolved. Do it again, this time pay more attention and minimize the change. I pointed out what you did wrong. I did not execute your wrong code to tell you where the algorithm went wrong, I just avoided your mistakes.
—SA
Gilmore Sacco 27-Dec-12 8:10am    
I managed to get it to work. Thx for your help sir.
Sergey Alexandrovich Kryukov 27-Dec-12 12:17pm    
You are very welcome. I was pretty sure you would do.
These days, it's a particular pleasure to help someone who actually can use help and make things working.

(To understand what I mean, please compare with the comments to this answer (not even mine):
http://www.codeproject.com/Answers/515222/problemplusinplusconnectionstring?cmt=372011#answer1
See what's going on, often..? :-)

Good luck, very best wishes, call again.
—SA
Gilmore Sacco 27-Dec-12 13:58pm    
OMG some people simply do not get it. People like you are helping becase they are kind not because this is your job and even if it was your job,any issue can be tackled i a civilized manner.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900