Common algorithm problems for arrays

Source: Internet
Author: User

1. Find the maximum, minimum, and average values of an array element

2. Copying and inversion of arrays

Scenario 1:


Scenario 2: (How to implement Replication)


Inverse of the array:
Inverse of an array element
for (int i = 0;i < arr.length/2;i++) {
int temp = Arr[i];
Arr[i] = arr[arr.length-1-i];
Arr[arr.length-1-I] = temp;
// }
for (int x = 0, y = arr.length-1; x < y; + x, y--) {
int temp = arr[x];
ARR[X] = Arr[y];
Arr[y] = temp;
}

Extension: String str = "ABCDEFG";

Sort the array:
Insert Sort
Direct Insert Sort, binary insert sort, shell sort

Exchange sort
Bubble sort, quick sort (or partition exchange sort)

Select sort
Simple select sort, heap sort

Merge sort

Base sort


//Use bubble sort to arrange array elements from small to large
//for (int i = 0; i < arr.length-1; i++) {
//for (int j = 0; J < arr.length-1-I; j + +) {
//if (Arr[j] > arr[j + 1]) {
//int temp = arr[j];
//arr[j] = arr[j + 1];
//arr[j + 1] = temp;
//}
//}
//}
////array elements from small to large using the direct selection sort
//for (int i = 0; i < arr.length-1; i++) {
//int t = i;//default i is the smallest
//for (Int j = I;j < arr.length;j++) {
////Once after I find that there is a smaller element than that, record the lower corner of the element
//if (Arr[t] > Arr[j]) {
//t = j;
//}
//}
//if (t! = i) {
//int temp = arr[t];
//arr[t] = arr[i];
//arr[i] = temp;
//}
//}

You can also call: Arrays Tool class: Arrays.sort (arr);

Common algorithm problems for arrays

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.