JAVA 5th course (sorting + Binary Search + lookup)

Source: Internet
Author: User
Tags string find

JAVA 5th course (sorting + Binary Search + lookup)

PS: algorithms are non-linguistic


Sort:

1. Extraction

2. Switch location


Import javax. swing. text. defaultEditorKit. insertBreakAction; import org. omg. cosNaming. namingContextExtPackage. addressHelper; public class Main {public static void main (String [] args) {// int [] B = new int [] {1, 2, 3, 4, 5 }; same as int B [] = {,}; // same as bubblesort (B); System. out. println ("Bubble sorting result:"); print (B); int c [] =, 3, 6}; seletsort (c); System. out. println ("the sorting result is as follows:"); print (c); int d [] =, 3, 6}; Insersort (d); System. out. println ("\ n insert sorting result:"); print (d);} static void Insersort (int B []) {int len = B. length; for (int I = 1; I
 
  
= 0 & B [j]> t; j --) {B [j + 1] = B [j];} B [j + 1] = t ;}}} static void swap (int x, int y, int [] B) {int t = B [x]; B [x] = B [y]; B [y] = t;}/* static void swap (int a, int B). Do not write this method, but simply exchange two numbers, * But the two numbers in array B are not exchanged {int t = a; a = B; B = t;} */static void bubblesort (int [] B) {int len = B. length; for (int I = 0; I
  
   
B [j + 1]) {swap (j, j + 1, B) ;}}} static void seletsort (int a []) {int len =. length; for (int I = 0; I
   
    
A [j]) {swap (I, I + 1, a) ;}}} static void print (int B []) {for (int I = 0; I
    
     

     

Binary Search

Import java. util. export; import javax. swing. text. defaultEditorKit. insertBreakAction; import org. omg. cosNaming. namingContextExtPackage. addressHelper; public class Main {public static void main (String [] args) {// int [] B = new int [] {1, 2, 3, 4, 5 }; when cin = new partition (System. in); int B [] = new int [11]; for (int I = 0; I <11; I ++) {B [I] = cin. nextInt ();} bubblesort (B); System. out. println ("the Bubble sorting result is as follows:"); print (B); System. out. print ("Enter the value you want to search for:"); int n; n = cin. nextInt (); boolean flag = B _search (n, B); if (flag = true) System. out. println ("YES"); elseSystem. out. println ("NO"); cin. close ();} static void swap (int x, int y, int [] B) {int t = B [x]; B [x] = B [y]; B [y] = t;} static boolean B _search (int n, int B []) {int low = 0, high = B. length-1; while (low <= high) {int mid = (low + high)/2; if (B [mid] = n) {return true ;} else if (B [mid]> n) {high = mid-1 ;}else {low = mid + 1 ;}} return false ;} static void bubblesort (int [] B) {int len = B. length; for (int I = 0; I
      
       
B [j + 1]) {swap (j, j + 1, B) ;}}} static void print (int B []) {for (int I = 0; I
       
        


Secondary search: Interview Questions

Give an ordered array. If an element is stored in the array and the array is still ordered, How can I obtain the badge of the element storage?

Import java. util. arrays; import java. util. export; import javax. swing. text. defaultEditorKit. insertBreakAction; import org. omg. cosNaming. namingContextExtPackage. addressHelper; public class Main {public static void main (String [] args) {// int [] B = new int [] {1, 2, 3, 4, 5 }; when cin = new partition (System. in); int B [] = new int [11]; for (int I = 0; I <11; I ++) {B [I] = cin. nextInt ();} bubblesort (B); System. out. println ("The Bubble sorting result is as follows:"); print (B); System. out. print ("Enter the value you want to search for:"); int n; n = cin. nextInt (); System. out. println ("handwritten Binary Search:"); int flag = B _search (n, B); System. out. println ("subscript: \ t" + flag); int wz = cin. nextInt (); int indx = Arrays. binarySearch (B, wz); // return a negative insert point subscript minus 1 // return a negative number, which indicates that the negative insertion point does not exist. If I put this element in it, where is the insertion point? a negative number refers to the negative insertion point subscript, and-1 // returns a positive number, which indicates the subscript/* why is it reduced by 1? * If the element to be inserted is smaller than the first element of the array, if the value is not reduced by 1, 0 is returned, and 0 is returned. Does this number mean that it does not exist, therefore, we need to subtract 1 **/System. out. println ("Call Binary Search:"); System. out. println ("subscript: \ t" + indx); cin. close ();} static void swap (int x, int y, int [] B) {int t = B [x]; B [x] = B [y]; B [y] = t;} static int B _search (int n, int B []) {int low = 0, high = B. length-1; while (low <= high) {int mid = (low + high)/2; if (B [mid] = n) {return mid ;} else if (B [mid]> n) {high = mid-1 ;}else {low = mid + 1 ;}} return low ;} static void bubblesort (int [] B) {int len = B. length; for (int I = 0; I
         
          
B [j + 1]) {swap (j, j + 1, B) ;}}} static void print (int B []) {for (int I = 0; I
          
           


Lookup Method


Import java. util. usage; public class Main {/** use the lookup Method * // * application of the lookup method: * regular, you can use the lookup table method **/public static void main (String [] args) {using cin = new using (System. in); {int x = cin. nextInt ();/** query week **/System. out. println (Find (x);/** query month **/int y = cin. nextInt (); System. out. println (Findd (y); cin. close () ;}} public static String Find (int x) {if (x> 7 | x <0) {return "input error ";} string [] week = {"Monday = Monday", "Tuesday = Tuesday", "Wednesday", "Thurseday", "Friday = Friday ", "Saturday = Saturday", "Sunday = Sunday"}; return week [x-1];} public static String Findd (int x) {if (x> 12 | x <0) {return "input error";} String [] mon = {"January = January", "February = Febary", "March = March", "April = Hangzhou L ", "May = May", "May June = June", "May July = July", "May August = August", "May September = Spetember", "May October = October ", "November = November", "December = December"}; return mon [x-1];}


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.