Advertisement
cgorrillaha

Untitled

Jan 14th, 2022
1,259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.10 KB | None | 0 0
  1. 1.  write a method to check to see if the elements in an int array are always increasing.
  2.  
  3. public static boolean arrayAcsending(int[] nums){
  4.     boolean isAscending=true;
  5.    
  6.     in count=1;
  7.    
  8.     while(isAscending&&count<nums.length){
  9.         if(nums[count]<nums[count-1]){
  10.             isAscending=false;
  11.         }
  12.     }
  13.    
  14.     return isAscending;
  15. }
  16.  
  17. 2. reverse an array
  18. public static void reverseArray(int [] nums){
  19. /*SWAP!
  20.   step 1: create a temp variable and store element at current index
  21.   step 2: assign value from index length-i-1 into current index
  22.   step 3: assign value from the temp variable into index length-i-1
  23. */
  24.     for(int i=0; i<nums.length/2; i++){
  25.         int temp=nums[i];
  26.         nums[i]=nums[nums.length-i-1];
  27.         num[nums.length-i-1]=temp;
  28.     }
  29. }
  30.  
  31. 3. shuffle an array
  32. public static void shuffleNums(int[] nums){
  33.  
  34.     for(int i=0; i<nums.length-1; i++){
  35.         //get min and max for generating rand range
  36.         int max=nums.length-1;
  37.         int min=i;
  38.        
  39.         //generate rand
  40.         int rand=(int)(Math.random()*(max-min+1))+min;
  41.        
  42.         //swap nums[i] with nums[rand]
  43.         int temp=nums[rand];
  44.         nums[rand]=nums[i];
  45.         nums[i]=temp;
  46.     }
  47.    
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement