Basic summary of C language (ii)----------Array Summary (priority sorting algorithm)

Source: Internet
Author: User

?? Write a program that initializes an array. Requires an array length of 10, a random number with a value of 0-9, and one occurrence for each number. Idea: 1, creating an array of 2, creating a random number
3, save in array nums[i++] = ...
4, use the loop to create the data and add it to the array. How many loops? while (I < 10)
5, create a number, determine if there is a number in the array, if not join, if any, re-create
6, how do you tell if a number is in an array or not in an array?
int nums[] = {n/a};
3 in the not, 4 in the not?
Iterate, and look until the end of the traversal, if there is no such number, it means that there is no array initialization? Implemented in code: int Nums[len], i = 0, temp, index;
while (I < 10) {
temp = Arc4random_uniform (10);
Loops are compared by random numbers and array elements to see if they are equal
for (index = 0; index < i; index++) {
if (temp = = Nums[index]) {
Break
}
}
index = = I shows that the For loop ends, and the random number and array elements are not equal
if (index = = i) {
nums[i++] = temp;
}
}
The loop is over and the random number is printed.
for (int j = 0; J <; J + +) {
printf ("%d\t%d\n", J + 1, nums[j]); }   ?? Bubble sort void Bubblesort (int nums[], int lengh) {
for (int i = 0; i < lengh-1; i++) {
for (int j = 0; J < lengh-1-I; j + +) {
if (Nums[j] > nums[j + 1]) {
int temp = Nums[j];
NUMS[J] = nums[j + 1];
Nums[j + 1] = temp;
}
}
}
} ?? Select sort void Choosesort (int nums[], int length) {
for (int i = 0; i < length-1; i++) {
for (int j = i; j < length; J + +) {
if (Nums[j] < nums[i]) {
int temp = Nums[j];
NUMS[J] = nums[i];
Nums[i] = temp;
}
}
}
} ?? Select sort optimization: int min =-1;
for (int j = 0; J < 4-1; J + +) {
min = j; Subscript (index) with minimum value recorded
for (int i = j + 1; i < 4; i++) {
if (Nums[min] > Nums[i]) {
min = i;
}
}

To exchange the smallest item with article J
if (min! = j) {
temp = Nums[min];
Nums[min] = Nums[j];
NUMS[J] = temp;
}    }  ?? Finding data by dichotomypremise: Arrays are arranged from small to largeDichotomy is a compromise to seek, the target data is greater than the middle number, in the right half of the search, loop execution ...
Assuming that the target data is found, it returns its subscript, and returns 1 if it is not found.
int indexOf (int nums[], int length, int keydata) {//Keydata represents the target data to find
Because the number of times to execute the loop is unknown, a while loop is used to implement
int low = 0, high = length-1,mid;
while (low <= high) {
Mid = (low + high)/2;
if (Keydata = = Nums[mid]) {
return mid;
}else if (Keydata > Nums[mid]) {
Low = mid + 1;
}else high = mid-1;
}
return-1;}

Basic summary of C language (ii)----------Array Summary (priority sorting algorithm)

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.