In this article we are going to see the Generic Quick Sorting implementation using C#.
Quick Sort :
Quick Sort is a Divide and conquer algorithm, It divides the collection int to two sub sets based on the pivot element selected, one subset is a collection elements which is smaller than pivot, another subset is collection of elements consists of larger than pivot.then each subsets are undergoes following steps again.Entire sort can be done in O(log n)
Steps :
1. Consider a element as Pivot from the List.
2. ReOrder the list so the elements which are lesser than pivot arrange previous in the order of the pivot and the elements which are higher than pivot arrange next in the order of the pivot.
3. Recursively apply the above steps to each two subsets individually.
Algorithm :
Now Let we see this implementation in C#
First Derive a Generic List<T> which is comparable, Then add collection to it ,Add a additional method to it as Order to order a collection based on the Quick Sort by implementing the algorithm. Following type in Generic so any data type can be sorted using this.
Now create a Two list one with int type other with string type and order the collection using Quick sort.
Output :
From this article you can learn the Generic Quick Sorting algorithm in C# and the process of algorithm.
Quick Sort :
Quick Sort is a Divide and conquer algorithm, It divides the collection int to two sub sets based on the pivot element selected, one subset is a collection elements which is smaller than pivot, another subset is collection of elements consists of larger than pivot.then each subsets are undergoes following steps again.Entire sort can be done in O(log n)
Steps :
1. Consider a element as Pivot from the List.
2. ReOrder the list so the elements which are lesser than pivot arrange previous in the order of the pivot and the elements which are higher than pivot arrange next in the order of the pivot.
3. Recursively apply the above steps to each two subsets individually.
Algorithm :
function quicksort('array')
if length('array') ≤ 1
return 'array'
for each 'x' in 'array'
if 'x' ≤ 'pivot' then append 'x' to 'less'
else append 'x' to 'greater'
return
concatenate(quicksort('less'),
list('pivot'), quicksort('greater'))
Now Let we see this implementation in C#
First Derive a Generic List<T> which is comparable, Then add collection to it ,Add a additional method to it as Order to order a collection based on the Quick Sort by implementing the algorithm. Following type in Generic so any data type can be sorted using this.
class Quicksort<T>:List<T> where
T : IComparable
{
public List<T>
Order()
{
return SortList(this.ToList<T>());
}
public List<T> SortList(List<T>
values)
{
List<T> _left = new
List<T>();
List<T> _right = new
List<T>();
if (values.Count() > 1)
{
T pivot = values[values.Count - 1];
values.RemoveAt(values.Count - 1);
foreach (T each in
values)
{
if (each.CompareTo(pivot) == 1)
{
_right.Add(each);
}
else
{
_left.Add(each);
}
}
return MergeList(SortList(_left), pivot,
SortList(_right));
}
return values;
}
private List<T>
MergeList(List<T> left, T pivot, List<T> right)
{
left.Add(pivot);
left.AddRange(right);
return left;
}
}
class Program
{
static void Main(string[] args)
{
Quicksort<int>
_sorting = new Quicksort<int>();
_sorting.AddRange(new int[] { 3,1,8,45,2,5,7});
Console.WriteLine("Ordering
the numbers using Quick Sort");
foreach (int s in _sorting.Order())
{
Console.WriteLine(s);
}
Quicksort<string>
_names = new Quicksort<string>();
_names.AddRange(new string[]{"Rajesh","Suresh","Pavi","Anna","Ganesh","Sathish","Kani","Hanish","Babu","Dilli"});
Console.WriteLine();
Console.WriteLine("Ordering
the names using Quick Sort");
foreach (string s in
_names.Order())
{
Console.WriteLine(s);
}
Console.Read();
}
}
Output :
From this article you can learn the Generic Quick Sorting algorithm in C# and the process of algorithm.
ReplyDeleteIt is really a great work and the way in which u r sharing the knowledge is excellent.
Thanks for helping me to understand basic concepts. As a beginner in Dot Net programming your post help me a lot.Thanks for your informative article. Dot Net Training in chennai | Dot Net Training in velachery
This is good site and nice point of view.I learnt lots of useful information.
ReplyDeleteClick here:
Angularjs training in chennai
Click here:
angularjs training in bangalore
Click here:
angularjs training in online
Click here:
angularjs training in Annanagar
Really nice experience you have. Thank you for sharing. It will surely be an experience to someone.
ReplyDeleteClick here:
Microsoft azure training in chennai
Click here:
Microsoft azure training in online
Click here:
Microsoft azure training in tambaram
Click here:
Microsoft azure training in chennai
Click here:
Microsoft azure training in annanagar
Great post! I am actually getting ready to across this information, It’s very helpful for this blog.Also great with all of the valuable information you have Keep up the good work you are doing well.
ReplyDeleteDevops training in tambaram|Devops training in velachery|Devops training in annanagar|Devops training in sholinganallur
I appreciate your efforts because it conveys the message of what you are trying to say. It's a great skill to make even the person who doesn't know about the subject could able to understand the subject . Your blogs are understandable and also elaborately described. I hope to read more and more interesting articles from your blog. All the best.
ReplyDeleteData Science training in rajaji nagar | Data Science with Python training in chenni
Data Science training in electronic city | Data Science training in USA
Data science training in pune | Data science training in kalyan nagar
I recently came across your blog and have been reading along. I thought I would leave my first comment.
ReplyDeletejava training in tambaram | java training in velachery
java training in omr | oracle training in chennai
My partner and I stumbled over here different website and thought I might as well check things out. I like what I see so now I’m following you. Look forward to checking out your web page repeatedly.
ReplyDeleteiosh course in chennai
feeling happy to read this post
ReplyDeletebest azure certification training in chennai
Wonderful post. Thanks for taking time to share this information with us.
ReplyDeleteAzure Training in Chennai
Microsoft Azure Training in Chennai
R Programming Training in Chennai
Big Data Analytics Courses in Chennai
Data Science course in Chennai
RPA course in Chennai
Azure Training in OMR
Azure Training in Porur
Thank you for excellent article.
ReplyDeletePlease refer below if you are looking for best project center in coimbatore
final year projects in coimbatore
Spoken English Training in coimbatore
final year projects for CSE in coimbatore
final year projects for IT in coimbatore
final year projects for ECE in coimbatore
final year projects for EEE in coimbatore
final year projects for Mechanical in coimbatore
final year projects for Instrumentation in coimbatore
Excellent post. I learned a lot from this blog and I suggest my friends to visit your blog to learn new concept about technology.
ReplyDeleteAngularJS Training in Chennai
AngularJS course in Chennai
ReactJS Training in Chennai
AngularJS Training in Anna Nagar
AngularJS Training in T Nagar
I have been meaning to write something like this on my website and you have given me an idea. Cheers.
ReplyDeleteangularjs online training
apache spark online training
informatica mdm online training
devops online training
aws online training
Thanks for sharing valuable Information, I really very impressive on your blog. I hope you continue on blogging job.
ReplyDeleteI would like thank you for creating a post learn hacking online
ReplyDeleteNice blog..
ReplyDeletefinal year project proposal for information technology
free internship for bca
web designing training in chennai
internship in coimbatore for ece
machine learning internship in chennai
6 months training with stipend in chennai
final year project for it
inplant training in chennai for ece students
industrial training report for electronics and communication
inplant training certificate
Useful information...
ReplyDeleteinplant training in chennai
inplant training in chennai
inplant training in chennai for it.php
Australia hosting
mexico web hosting
moldova web hosting
albania web hosting
andorra hosting
australia web hosting
denmark web hosting
I really like the dear information you offer in your articles. I’m able to bookmark your site and show the kids check out up here generally.thanks.
ReplyDeleteAi & Artificial Intelligence Course in Chennai
PHP Training in Chennai
Ethical Hacking Course in Chennai Blue Prism Training in Chennai
UiPath Training in Chennai
Great post. Thanks for sharing this wonderful information.
ReplyDeleteAngularJS training in chennai | AngularJS training in anna nagar | AngularJS training in omr | AngularJS training in porur | AngularJS training in tambaram | AngularJS training in velachery
Really it was an awesome article,very interesting to read.You have provided an nice article,Thanks for sharing. oracle training in chennai
ReplyDeleteThe article was absolutely fantastic! Lot of great information which can be helpful in some or the other way. Keep updating the blog, looking forward for more contents.
ReplyDeleteby cloudi5 is the best Web Design Company in Coimbatore
Hi, I am John Smith I am Web Developer, It is an amazing blog thanks for the sharing the blog. Frantic infotech provide the phonegap app development such as an information about software development for costumer service. Frantic infotech also provide the ionic app development. The development of advanced web applications is Orient Software’s specialty and we will successfully fulfill all your web application development requirements, from small-sized to wider-ranged projects.
ReplyDelete