In: Computer Science
Java: Create a class and name it MyArray and implement following method. * NOTE: if you need more methods, including insert(), display(), etc. you can also implement those Method name: getKthMin(int k) This method receives an integer k and returns k-th minimum value stored in the array. * NOTE: Items in the array are not sorted. If you need to sort them, you can implement any desired sorting algorithm (Do not use Java's default sorting methods). Example: Items in the array: 4, 6, 9, 10, 3, 11, 1, 2, 5, 7, 8 call the method to get 3rd min value myArray.getKthMin(3); -> this will return 3
I have written where the array is in dynamic in nature .
Intially the size of the array will be 0 and once you start adding
elment it will increase its size dymanically (Twice of its size)
.
It has the follwing methods :
Code :
import java.util.*;
class  MyArray{
    
    private int array[] ;
    private int count ;
    private int size ;
    
    
    public MyArray(){
        array = new int[1];
        count = 0 ;
        size = 1 ;
    }
    
    
    //function add an element at the end of array 
    public void addElement(int data){
        if(count==size){
            growArraySize() ; 
        }
        
        array[count] = data ;
        count++; 
    }
    
    public void growArraySize(){
        int temp[] = null ;
        if(count == size){
            temp = new int[size*2];
            
            for(int i = 0 ; i<size ; i++){
                temp[i] = array[i] ;
            }
        }
        
        array = temp ; 
        size = size * 2 ; 
        
    }
    
    
    //function shrink size of array which block unnecessary remove them 
    
    public void shrinkSize(){
        int temp[] = null ;
        if(count>0){
            temp = new int[count] ;
            for(int i = 0 ;i<count ;i++){
                temp[i] = array[i];
            }
            size = count ;
            array = temp ; 
        }
    }
    
    
    public void removeLastElement(){
        if(count>0){
            array[count - 1] = 0 ;
            count-- ;
        }
    }
    
    public void getKthMin(int k){
        
        // Sort the given array 
        int tempArray = array.clone();
        tempArray = sortArray(tempArray); 
  
        // Return k'th element in 
        // the sorted array 
        return tempArray[k - 1]; 
    }
    
    public int[] sortArray(int tempArray[]){
      int n = tempArray.length;
      int temp = 0;
      for(int i = 0; i < n; i++) {
         for(int j=1; j < (n-i); j++) {
            if(tempArray[j-1] > tempArray[j]) { 
               temp = arr[j-1]; 
              tempArray[j-1] = tempArray[j];
               tempArray[j] = temp;
            } 
         } 
      }
      return tempArray; 
   }
    
    
    
    public static void main(String[] args) throws Exception {
        MyArray array = new MyArray();
        array.addElement(10);
        array.addElement(80);
        array.addElement(8);
        array.addElement(9);
        array.addElement(34);
        array.addElement(98);
        array.addElement(23);
        array.addElement(87);
        array.addElement(65);
        
        for(int i = 0 ;i<array.size ; i++){
            System.out.print(array.array[i]+" ");
        }
        System.out.println();
        
        System.out.print(array.getKthMin(2)); 
        
    }
}
Hope you got your answer !
if you still have any doubts please let me know in comment box . Thanks ! happy learning ;)