在keyp中实现bubblesort [英] Implement a bubblesort in a keyp

查看:91
本文介绍了在keyp中实现bubblesort的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C#的新手,需要在程序中编写一个bubblesort算法。这个想法是,bubblesort应该对人口中的条目(州+人口)进行排序。



我宣布一个清单 - 请看下面:



语言是C#...



I'm a newbie in C# and need to write a bubblesort-algorithm in a program. The idea is that the bubblesort should sort the entries (state + population) on population.

I declare a list - please see below:

Language is C#...

var Members = new List<KeyValuePair<string, int>>();
Members.Add(new KeyValuePair<string, int>("Texas", 705));
Members.Add(new KeyValuePair<string, int>("Ohio", 901)):
Members.Add(new KeyValuePair<string, int>("Alabama", 210));
var m = Members.ToArray();





然后我运行一个预排序 - 排序之前 - 用foreach(m对)并写入行..



最后我需要实现一个bubblesort算法对上述内容进行排序 - 以便按顺序显示阿拉巴马州,德克萨斯州和俄亥俄州。我不想使用通常的m.Sort(); - 我必须编写bubblesort算法 - 但我有点陷入其中!!



我的问题是实现KeyValuePair - 我无法抓住它 - 是吗可能有人可以告诉我如何?



以上我宣布一个公共静态void QuickSort BubbleSort(KeyValuePair< string,int> [] input,int left,int right)



从这里 - 我迷路了......呵呵 - 所以我很乐意看到一些好的建议:)提前致谢。



我尝试了什么:



这是我尝试过的代码:



Then I run a pre-sort - before sort - with a foreach (pair in m) and writes the line..

Finally I need to implement a bubblesort-algorithm that sorts the above - so that it shows Alabama, Texas and then Ohio in that order. I do not want to use the usual m.Sort(); - I have to write the bubblesort algorithm - but am a bit stuck in it!!

My problem is to implement KeyValuePair - I cannot get hold on it - is it possible that anyone could show me how to?

Above main I declare a public static void QuickSortBubbleSort(KeyValuePair<string, int>[] input, int left, int right)

From here - I'm lost... hehe - so I would be happy to see some good advice :) Thanks in advance.

What I have tried:

This is the code I've tried with:

class Program
{
     public static void BubbleSort(KeyValuePair<string, int>[] input, int left, int right)
     {
         if (left < right)
         {
             int q = Del(input, left, right);
             BubbleSort(input, left, q - 1);
             BubbleSort(input, q + 1, right);
         }
     }


     private static int Del(KeyValuePair<string, int>[] input, int left, int right)
     {
         for (int pass = 1; pass <= input.Length - 2; pass++)
         {
             for (int i = 0; i <= input.Length -2; i++)
             {
                 var temp = input[i + 1];
                 input[i + 1] = input[i];
                 input[i] = temp;
             }
         }
         return input.Length;
     }


     static void Main()
     {
         var Members = new List<KeyValuePair<string, int>>();
         Members.Add(new KeyValuePair<string, int>("Texas", 405));
         Members.Add(new KeyValuePair<string, int>("Indiana", 895));
         Members.Add(new KeyValuePair<string, int>("Utah", 966));
         Members.Add(new KeyValuePair<string, int>("Oregon", 203));

         var clist = Members.ToArray();
         foreach (var pair in clist)
         {
             Console.WriteLine(pair);
         }

         BubbleSort(clist, 0, Members.Count - 1);
         foreach (var pair in clist)
         {
             Console.WriteLine(pair);
         }

         Console.WriteLine("Tryk ENTER for Exit");
         Console.ReadLine();

     }
}

推荐答案

你冒泡排序不是冒泡排序所有。

你需要研究这个主题并重新改写这个功能。

泡泡排序 - 维基百科,免费的百科全书 [ ^ ]
You bubble sort is not bubble sort at all.
You need to study the subject and rewrute the function.
Bubble sort - Wikipedia, the free encyclopedia[^]


您可以使用 System.Collections.Generic.KeyValuePair< string,int> (或者它可能是其他整数类型, uint ulong ):
You can use System.Collections.Generic.KeyValuePair<string, int> (or it could be some other integer type, uint or ulong):
var members = new System.Collections.Generic.List<System.Collections.Generic.KeyValuePair<string, uint>>();
members.Add(new KeyValuePairSystem.Collections.Generic.KeyValuePair<string, int>("Texas", 705));
// ...





对不起,我刚刚回答了您的问题,但是甚至没有看到您的排序实施。我知道您需要自己实现它,而不是使用可用的.NET BCL方法。但是一步一步。首先,理解你对他们使用的泛型的理解。



顺便说一下, KeyValuePair 实例很少被直接申请。通常,它们基于关联容器(通过哈希码和实现)出现在集合类的迭代器中。但是这种类型适用于你,为什么不使用呢?



-SA



Sorry, I just answered your immediate question, but did not even look at your implementation of sorting. I understand that you are required to implement it yourself, instead of using available .NET BCL methods. But make one step at a time. First, sort out your understanding of generics they use.

By the way, KeyValuePair instances are rarely used by applications directly. Usually, they appear in the iterators of the collection classes based on associative containers (implemented via hash codes and buckets). But it this type is available and suits you, why not using it?

—SA


您可以使用 SortedDictionary 来保持其键始终排序:

http://www.dotnetperls.com/sorteddictionary [ ^ ]



作为关键,你应该使用人口,而不是国家,如果你想按人口排序。
You can use a SortedDictionary which keeps its keys always sorted:
http://www.dotnetperls.com/sorteddictionary[^]

As the key you should use the population, not the state, if you want it sorted on population.


这篇关于在keyp中实现bubblesort的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆