[Translation] C # data structures and algorithms-Chapter 3 basic sorting algorithms

Source: Internet
Author: User

Chapter 2 Basic Sorting Algorithm

The two most common operations for data storage on a computer are sorting and searching. Since the beginning of the computer industry, this is clear, so sorting and searching are the two most widely studied operations in the computer science field. Most of the data structures discussed in this book are mainly designed to make data storage sorting and/or searching in the structure easier and more efficient.

This chapter describes the basic algorithms for sorting and searching data. These algorithms only need data structures like arrays. The only "advanced" computer technology is loop. This chapter also introduces the informal techniques used in the whole book to analyze the speed and efficiency of different algorithms.

Sorting Algorithm

Most of the data in our daily work is sorted. We search for definitions in a dictionary in alphabetical order. We can find a phone number in the book in alphabetical order of the last name. The post office sorts emails in multiple ways-first by zip code, then by address, and then by name. Sorting is a basic method for processing data. We should study it carefully.

As we mentioned earlier, a lot of work has been put into research on different sorting technologies. Although some very advanced sorting algorithms have been invented, you should first learn some simple sorting algorithms. These algorithms include insert sorting, Bubble sorting, and select sorting. These algorithms are easy to understand and use. Although they are not the best sorting algorithms in any case, they are the best algorithms to be used for small datasets or other special cases.

A data test framework

To check these algorithms, we first need a test framework to implement and test them. We need to construct a class to encapsulate the common operations executed on an array-element insertion, element access, and display the array content. The following code is used:

Class CArray

{

Private Int[] Arr;

Private IntUpper;

Private IntNumElements;

 

PublicCArray ()

{}

 

PublicCArray (IntSize)

{

Arr =New Int[Size];

Upper = size-1;

NumElements = 0;

}

Public VoidInsert (IntItem)

{

Arr [numElements] = item;

NumElements ++;

}

Public VoidDisplayElements ()

{

For(IntI = 0; I <= upper; I ++)

Console. Write (arr [I] +"");

}

Public VoidClear ()

{

For(IntI = 0; I <= upper; I ++)

Arr [I] = 0; numElements = 0;

}

}

 

Class Class1

{

Static VoidMain ()

{

CArrayNums =New CArray();

For(IntI = 0; I <= 49; I ++)

Nums. Insert (I );

Nums. DisplayElements ();

}

}

Enter the following code:

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 19 20 21 22 23 24 26 26 28 29 30 31 32 33 34 35 36 38 39 40 41 42 43 44 46 47 48 49

Before leaving the CArray class to check sorting and search algorithms, we need to understand how to actually store data to CArray objects. To better prove the operation of different sorting algorithms, the data in the array needs to be sorted randomly. The best way is to use a random number generator to assign each array element to the array to be tested.

In C #, you can use the Random class to create Random numbers. Objects of this class can generate random numbers. To instantiate a Random class, you need to input a seed to the class constructor. This seed can be considered as the upper limit of the range of values that can be generated by the random number generator.

This is another program that uses the CArray class to store numbers. Use the random number generator to select the data to be stored in the array.

Static VoidMain ()

{

CArrayNums =New CArray();

RandomRnd =New Random(100 );

For(IntI = 0; I <10; I ++)

Nums. Insert ((Int) (Rnd. NextDouble () * 100 ));

Nums. DisplayElements ();

}

The output of this program is as follows:

72 54 59 30 31 78 2 77 82 72

 

Bubble Sorting

The first Sorting Algorithm to be introduced is Bubble sorting. Bubble Sorting is one of the slowest available sorting algorithms, but it is also the easiest and easiest to understand and use, making it the best candidate for learning.

The name of this sort comes from an element, like a bubble, "floating" from an array to another end. Assume that you want to sort a series in ascending order, and the larger value will be moved to the right and the smaller value will be moved to the left. This behavior is performed multiple times by moving in the list and comparing adjacent values. If the left value is greater than the right value, they are exchanged.

Figure 3.1 shows the Bubble sorting. The two numbers (2 and 72) are inserted into the array of the previous example and highlighted in circles. You can see how 72 is moved from the beginning of the array to the center of the array, and how 2 moves through the middle of the array to the beginning of the array.

<

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.