最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

QuickSort和BubbleSort

SEO心得admin97浏览0评论
本文介绍了QuickSort和BubbleSort的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

当然,我正在处理某些代码.我的老师给了我这个Quicksort以便在我的代码中使用.不幸的是他没有空,所以我想我会问你... 在此代码中,我应调用main的哪种方法来完成快速排序? 我有一个更简单的代码,并且一直在进行比较以找出答案,我在其他代码中找到了答案,但是我不能在这一代码中找到答案.我认为它的qs(T []值).我以为会是sort(),但是在调用该方法时遇到了问题.

Of course Im working on some code. I was given this Quicksort to use in my code by my instructor. Unfortunately he isnt available so I thought I would ask you... In this code, which method do I call to main to complete the quicksort? I have an easier one and have been comparing to find out and I figured it out in my other code but I cant in this one. I think its qs(T[] values). I thought it would be sort() but I run into problems when I call that method.

public class QuickSort<T> where T : IComparable { T[] input; public T[] Output { get { return input; } } public void qs(T[] values) { input = new T[values.Length]; for (int i = 0; i < values.Length; i++) { input[i] = values[i]; Console.WriteLine(input[i]); Console.WriteLine(); } } // Set up a call to the actual Quicksort method. public void Sort() { Sorting(0, input.Length - 1); } public int getPivotPoint(int begPoint, int endPoint) { int pivot = begPoint; int m = begPoint + 1; int n = endPoint; while ((m < endPoint) && (input[pivot].CompareTo(input[m]) >= 0)) { m++; } while ((n > begPoint) && (input[pivot].CompareTo(input[n]) <= 0)) { n--; } while (m < n) { T temp = input[m]; input[m] = input[n]; input[n] = temp; while ((m < endPoint) && (input[pivot].CompareTo(input[m]) >= 0)) { m++; } while ((n > begPoint) && (input[pivot].CompareTo(input[n]) <= 0)) { n--; } } if (pivot != n) { T temp2 = input[n]; input[n] = input[pivot]; input[pivot] = temp2; } return n; } public void Sorting(int beg, int end) { if (end == beg) { return; } else { int pivot = getPivotPoint(beg, end); if (pivot > beg) Sorting(beg, pivot - 1); if (pivot < end) Sorting(pivot + 1, end); } } } }

任何帮助将不胜感激.谢谢

Any help will be appreciated. Thanks

推荐答案

您首先必须调用"qs"以将要排序的数组填充到对象中.然后,您可以调用排序"来进行排序,然后可以调用输出"来获取排序后的值. Hi, you first have to call "qs" to fill the object with the array you want to be sorted. Then you call "Sort" to do the sorting and then you can call "Output" to get the sorted values.

您可以像这样使用此类: You use this class like; QuickSort<int> sortObject = new QuickSort<int>(); sortObject.qs(new int[] {20,56,45,12 }); sortObject.Sort(); int[] test = sortObject.Output; foreach (int value in test) { Console.WriteLine(value); }

qs函数加载值数组,然后调用Sort实际上对数组进行排序,然后您可以通过Output属性获得排序后的数组.

the qs function loads the array of values then calling Sort actually sorts the array and you get the sorted array out with the Output property.

谢谢大家.那是我的难题的最后一块.有效! :) Thanks guys. That was the last piece to my puzzle. It worked! :)

发布评论

评论列表(0)

  1. 暂无评论