Search and sort 03, select sort, search sort 03 select

Source: Internet
Author: User

Search and sort 03, select sort, search sort 03 select

Selecting sorting is an inefficient sorting algorithm. The general process is to traverse every element of the array. First, assume that the element at position 0 is the smallest, assign the value of index 0 to a variable that represents the index of the smallest element, for example, smallest, and traverse the elements after location 0. Once an element is found to be smaller than the element at location 0, assign the index of this element to smallest, continue traversing, and finally assign the index of the smallest element after location 0 to the smallest variable, swap the element at location 0 with that at location smallest, so that the minimum element is placed at location 0. Then, put the second and second smallest elements in the first position, put the third smallest element in the second position, and so on, and finally get an array in ascending order. As the elements of the array are cyclically traversed in sequence, individuals prefer to consider the selected sorting as linear sorting.

 

A custom class maintains an array of int [] type. The constructor defines the length of the array and initializes it. It also provides methods for printing and sorting.

   public class MyArray
    {
        private static int[] arr;
        private static Random r = new Random();
        public MyArray(int size)
        {
            arr = new int[size];
            for (int i = 0; i < size; i++)
            {
                arr[i] = r.Next(1, 100);
            }
        }
// Select the Sorting Algorithm
        public void Sort()
        {
Int smallest; // index of the smallest element
// The last index location does not need to be traversed, because the internal loop of the code segment contains the processing of the last index location.
            for (int i = 0; i < arr.Length - 1; i++)
            {
// Assign the index of the element currently traversed to smallest, That is, assuming that the element of the array currently traversed is the smallest element.
                smallest = i;
// Traverse all elements after the current traversal Element
// Obtain the index of the smallest element
                for (int index = i + 1; index < arr.Length; index++)
                {
                    if (arr[index] < arr[smallest])
                    {
                        smallest = index;
                    }
                }
// Swap the current traversal element with the minimum element
                Swap(i, smallest);
// Print the order each time
                Print();
            }
        }
// Swap the elements at two locations
        public void Swap(int first, int second)
        {
            int temp = arr[first];
            arr[first] = arr[second];
            arr[second] = temp;
        }
// Print array elements
        public void Print()
        {
            foreach (var item in arr)
            {
                Console.Write(item + " ");               
            }
            Console.WriteLine("\n");
        }
    }

 

Client call.

    class Program
    {
        static void Main(string[] args)
        {
            MyArray myArray = new MyArray(8);
Console. Write ("Before sorting :");
            myArray.Print();
Console. WriteLine ("sorted :");
            myArray.Sort();
            Console.ReadKey();
        }
    }

 

Obviously, for the selection sorting, the External Loop carries on the n-1 iteration, the internal loop carries on the n-1 Iteration for the first time, the second carries on the N-2 Iteration ...... In terms of time complexity, small items and constant items are ignored, and the sorting is basically a square order, which is written as O (n² ).

 

The "Search and sorting" series include:

Search and sorting 01, linear search, time complexity, algorithm search and sorting 02, half-lookup and sorting 03, select sorting search and sorting 04, insert sorting
VB code for sequential searching and sorting ·

Cycle
You can also write more information. Other people can help you.
The simplest, search and sorting are all available.
Form, a button to start

Private Sub commandementclick ()
Dim I, j, t, a (1 To 10)
Randomize
Print "original array :"
For I = 1 To 10
A (I) = Rnd * 10
Print "a (" & I & ") =" & a (I) & Space (2 ),
If I Mod 2 = 0 Then Print
Next I
Print
For I = 1 To 9
For j = I + 1 To 10
If a (j) <a (I) Then
T = a (I)
A (I) = a (j)
A (j) = t
End If
Next j
Next I
Print "sorted array from small to large :"
For I = 1 To 10
Print "a (" & I & ") =" & a (I) & Space (2 ),
If I Mod 2 = 0 Then Print
Next I
End Sub

Data search and sorting

Sequential search
Half Lookup
Multipart search
Direct Search

Insert sort
Hill sorting
Select sort
Quick sorting

Hill sorting code

* P = array pointer; n = number of array elements
Void Hill_arrangement (int * p, int n)
{Int I, gap, j, temp;
Gap = n/2;
While (gap> = 1)
{For (I = gap; I <n; I ++)
{Temp = * (p + I );
J = I;
While (j> = gap & * (p + j-gap)> temp)
{* (P + j) = * (p + j-gap );
J = j-gap;
* (P + j) = temp;
}
}
Gap = gap/2;
}
}

Code for the half-fold search of an ordered table (returns the subscript)

Int binarysearch (int * p, int n, int k)
{Int low = 0, up = n-1, mid;
While (low <= up)
{Mid = (low + up)/2;
If (k = * (p + mid) return mid;
Else if (k <* (p + mid) up = mid-1;
Else low = mid + 1;
}
Return-1;

}

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.