Search icon CANCEL
Subscription
0
Cart icon
Cart
Close icon
You have no products in your basket yet
Save more on your purchases!
Savings automatically calculated. No voucher code required
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
C# Data Structures and Algorithms

You're reading from  C# Data Structures and Algorithms

Product type Book
Published in Apr 2018
Publisher Packt
ISBN-13 9781788833738
Pages 292 pages
Edition 1st Edition
Languages
Author (1):
Marcin Jamro Marcin Jamro
Profile icon Marcin Jamro
Toc

Sorting algorithms


There are many algorithms that perform various operations on arrays. However, one of the most common tasks is sorting an array to arrange its elements in the correct order, either ascending or descending. The topic of sorting algorithms involves many approaches, including selection sort, insertion sort, bubble sort, and quicksort, which will be explained in detail in this part of the chapter.

Selection sort

Let's start with the selection sort, which is one of the simplest sorting algorithms. The algorithm divides the array into two parts, namely sorted and unsorted. In the following iterations, the algorithm finds the smallest element in the unsorted part and exchanges it with the first element in the unsorted part. It sounds very simple, doesn't it?

To better understand the algorithm, let's take a look at the following iterations for an array with nine elements (-11, 12, -42, 0, 1, 90, 68, 6, -9), as shown in the following diagram:

To simplify the analysis, the bold line...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime