Inserts a data in a set of descending arrays, and when inserted, the data in the array is still sorted in descending order

Source: Internet
Author: User

  Analysis:  1, the array is fixed, is a reverse of the array  2, insert a data, where this data is inserted, to compare, and the array of all elements compared, here need a loop, because it is a descending array, So when the first encounter is smaller than oneself, then this position is to insert the position  3, because the previous step was occupied, then from the inserted data, the original data must be shifted to the right one

/** *  */ PackageCom.cn.u4;ImportJava.util.Scanner;/** * @authorAdministrator * Inserting learners ' scores into an ordered array * insert a data into a set of descending data, and after inserting, the data in the array is still sorted in descending order*/ Public classInsertnum { Public Static voidMain (string[] args) {//To ensure that the inserted data has a position, it must be one bit longer than the array, so define the array when you define it.        int[] nums=New int[6]; nums[0]=789; nums[1]=100; nums[2]=89; nums[3]=78; nums[4]=65; //let user input insert dataScanner input=NewScanner (system.in); System.out.print ("Please enter data:"); intWant=Input.nextint (); //Compare the data entered by the user with the data in the original array//find the place to insert, subscript, first time bigger than others        intIndex=-1;//Subscript Initial value, can be arbitrary, it will still be assigned in the back, so here just to not error to give an initial value, the divisor group subscript 0 .... 5 Outside, you can         for(inti=0;i<nums.length;i++){            if(want>Nums[i]) {Index=i;  Break;//as long as the lookup to find the first time than the input value is small, it is possible, so with break to end for loop}} System.out.println ("The subscript to be moved to is:" +index); //Insert the data, then the data is shifted to the right one bit, here to analyze,//because the num[5 defined above] is the 6th position is not used, is empty, then we want to move num[4] to num[5], and so on, at the index position in order to move to the right of a         for(inti=nums.length-1;i>0;i--){            if(index==i) {//move only the data from the beginning and back of the inserted coordinates                 Break; } Nums[i]=NUMS[I-1];//assign the previous position to the latter} Nums[index]=want;//assigns the input value to the position of the index coordinate//using an enhanced loop array         for(intnum:nums) {System.out.print (num+ "\ T"); }        //Output Array            }}

The

Inserts a data in a set of descending arrays, and when inserted, the data in the array is still sorted in descending order

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.