예제 #1
0
        public static int partition(List <CityRankings> l, int s, int e)
        {
            int i     = 0;
            int pivot = l.ElementAt <CityRankings>(e).rating;

            for (int j = 0; j < e; j++)
            {
                if (l.ElementAt <CityRankings>(j).rating <= pivot)
                {
                    CityRankings temp = l.ElementAt <CityRankings>(i);

                    l.Insert(i, l.ElementAt <CityRankings>(j));
                    l.RemoveAt(i + 1);

                    l.Insert(j, temp);
                    l.RemoveAt(j + 1);
                    i++;
                }
            }
            CityRankings temp1 = l.ElementAt <CityRankings>(i);

            l.Insert(i, l.ElementAt <CityRankings>(e));
            l.RemoveAt(i + 1);

            l.Insert(e, temp1);
            l.RemoveAt(e + 1);
            return(i);
        }
예제 #2
0
        public string[] sortCitiesByCrimeRate(string cities)
        {
            string[]            cityArray = Regex.Split(cities, ",");
            List <CityRankings> l         = new List <CityRankings>();

            foreach (string s in cityArray)
            {
                CityRankings c = new CityRankings(s);
                l.Add(c);
            }
            QuickSort           q            = new QuickSort();
            List <CityRankings> sortedCities = q.sort(l, 0, l.Count - 1);

            string[] sortedCityArray = new string[l.Count];
            for (int i = 0; i < sortedCities.Count; i++)
            {
                sortedCityArray[i] = sortedCities.ElementAt <CityRankings>(i).cityName + ", Total Crimes: " + sortedCities.ElementAt <CityRankings>(i).rating;
            }
            return(sortedCityArray);
        }