Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In any implementation language, which accepts a POST of a list of integers in JS

ID: 3786844 • Letter: I

Question

In any implementation language, which accepts a POST of a list of integers in JSON format and returns the list as a JSON object in sorted order. Any sort algorithm may be used. Example input in List (5, 35, 1, 272, 12, 0, -2, 121 Example output: out list [-2, 0, 125, 12, 12, 35, 272 algorithm quicksort time MS 52 The output JSON must also include the name of the algorithm and the amount of time taken to execute the sort, in milliseconds. Erroneous input (e.g. malformed SON) should be handled gracefully with an error message. Example error message Malformed JSON

Explanation / Answer

// program regarding json lists with sorting

// give more numbers to notice time elapsed.

import java.util.*;
public class JSON_Test{
   public static void main(String args[]){
       Scanner sc = new Scanner(System.in);
       System.out.println("** Enter input in Single line **");
       String input = sc.nextLine();
       //System.out.println(input);
       StringBuffer name = new StringBuffer();
       StringBuffer list_str = new StringBuffer();
       int[] original;
       int name_in=0;
       boolean name_flag = false;
       int i = 0;
       int len = input.length();
       while(i<len && input.charAt(i) == ' '){
           i++;
       }
       if(i==len){
           System.out.println("Incorrect input !!");
           System.exit(0);
       }
       if(input.charAt(i) == '{'){
           i++;
           while( i<len && input.charAt(i) == ' '){
           i++;
           }
           if(i==len){
           System.out.println("Incorrect input !!");
           System.exit(0);
           }
           if(input.charAt(i) == '"'){
               i++;
               while(i<len && input.charAt(i) != '"'){
                   name.append(input.charAt(i));
                   i++;
               }
               if(i==len){
                   System.out.println("Incorrect input !!");
                   System.exit(0);
               }
               i++;
               while( i<len && input.charAt(i) == ' '){
               i++;
               }
               if(i==len){
                   System.out.println("Incorrect input !!");
                   System.exit(0);
               }
               if(input.charAt(i) ==':'){
                   i++;
                   while( i<len && input.charAt(i) == ' '){
                   i++;
                   }
                   if(i==len){
                       System.out.println("Incorrect input !!");
                       System.exit(0);
                   }
                   if(input.charAt(i)=='['){
                       i++;
                       if(i==len){
                           System.out.println("Incorrect input !!");
                           System.exit(0);
                       }
                       while(i<len && input.charAt(i) != ']'){
                           list_str.append(input.charAt(i));
                           i++;
                       }
                       StringTokenizer st = new StringTokenizer(list_str.toString(),",");
                       original = new int[st.countTokens()];
                       while (st.hasMoreTokens()) {
                    try{
                        original[name_in] = Integer.parseInt(st.nextToken());
                        name_in++;
                    }
                    catch(Exception e){
                        System.out.println("Incorrect Input. Hint : Can't find "numbers" in the input");
                        System.exit(0);
                    }
                    }
                    i++;
                    while( i<len && input.charAt(i) == ' '){
                           i++;
                       }
                    if( i >= len || input.charAt(i) != '}' ){
                        System.out.println("Incorrect Input. Hint : Closing Tag");
                        System.exit(0);
                    }
                    Merge mms = new Merge();
                    long startTime = System.currentTimeMillis();
               mms.sort(original);
               long stopTime = System.currentTimeMillis();
                   long elapsedTime = stopTime - startTime;
               System.out.print("{ "outList" :[");
               for(int j=0; j<original.length;j++){
                System.out.print(original[j]);
                if(j != original.length-1){
                   System.out.print(",");
                }
               }
               System.out.print("], "algorithm" : "Mergesort", ");
               System.out.print(" "timeMs" : "+ elapsedTime +" } ");
                   }
               }
           }
           else{
               System.out.println("Incorrect Input. Hint : Can't find "inlist" Starting quote ' " ' ");
               System.exit(0);
           }
       }
       else{
           System.out.println("Incorrect Input. Hint : Can't find "JSON list" Starting brace - "{" ");
           System.exit(0);
       }
   }
}
class Merge{
private int[] array;
private int[] tempArr;
private int length;
Merge(){

}

public void sort(int inputArr[]) {
this.array = inputArr;
this.length = inputArr.length;
this.tempArr = new int[length];
MergeSort_Partition(0, length - 1);
}

private void MergeSort_Partition(int low, int high) {

if (low < high) {
int middle = low + (high - low) / 2;
MergeSort_Partition(low, middle);
MergeSort_Partition(middle + 1, high);
sortwith_Parts(low, middle, high);
}
}

private void sortwith_Parts(int low, int middle, int high) {

for (int i = low; i <= high; i++) {
tempArr[i] = array[i];
}
int i = low;
int j = middle + 1;
int k = low;
while (i <= middle && j <= high) {
if (tempArr[i] <= tempArr[j]) {
array[k] = tempArr[i];
i++;
} else {
array[k] = tempArr[j];
j++;
}
k++;
}
while (i <= middle) {
array[k] = tempArr[i];
k++;
i++;
}
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote