Objective
Two of the most common operations for storing data in a computer are sorting and searching. This is the beginning of the computer industry has been confirmed. This means that sorting and searching are two of the most worthwhile operations in the field of computer science. This book mentions the Xu
The main design purpose of multiple data structures is to make sorting and/or lookup simpler and more efficient for data to be stored within the structure.
This chapter describes the basic algorithms for sorting and finding data. These algorithms rely only on arrays as data structures, and the "advanced" programming techniques used are simply recursion. This chapter also introduces the methods used to analyze the speed and efficiency between different algorithms informally, which runs through the book.
1. Sorting algorithm
Most of the data that people have come into contact with in their daily lives is sorted. For example, query the dictionary definition in alphabetical order. Or look up the phone number in the phone book alphabetically by name. Or the Post office will follow these
Steps to sort and distribute messages: first, follow the postal code, then the street name, and finally, by name. Sorting is a very basic process in data processing, so it is worth studying seriously. As mentioned earlier, there is a very small amount of analysis of the operations of different sorting algorithms. Although some of the most ancient algorithms have been improved, it is still important to learn several simple sorting algorithms. These simple algorithms are insert sort algorithm, bubble sort algorithm and select sorting algorithm. Each of these algorithms is easy to understand and implement. These algorithms are not the best comprehensive algorithm for any situation, but they are the best available algorithms for a small number of data sets or other special cases.
1.1 Array Class test environment
To test these algorithms, we first need to construct a test environment that can implement and test the algorithm. A class is constructed to encapsulate some of the general operations of array processing, namely, element insertion, element access, and the groups that display the contents of the array. Here is the code for the program:
Before you leave the CArray class to start detecting sorting and lookup algorithms, let's discuss how to actually store the data within the CArray class object. In order to explain more effectively how different sorting algorithms are run, the data in the array needs
To be placed randomly. The best way to do this is to use the random number generator to assign to each element of the array. Random classes can be used in C # to produce random numbers. Objects of this type can produce random numbers. To instantiate a random object, you need to pass a seed to the constructor of this class. This seed is considered to be the upper bound of the range of random numbers that can be produced by a random number generator.
Here's another program that uses the CArray class to store numbers, and a random number generator to select the data stored in the array:
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Threading.Tasks;
Namespace Chapter2
{
class program
{
static void Main (string[] args)
{
CArray nums = new CArray (ten);
Random rnd = new Random (MB);
for (int i = 0; I <10; i++)
{
nums. Insert (Rnd. Next (0,100));
}
Nums. Displayelements ();
Console.readkey ();}}