Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Name: Mackenzie Cooper
- *V#: 00892515
- *Project Name: Marvel (project 1)
- *Latest Change: 9/24/2018
- *due date: 9/24/2019
- */
- import java.io.*;
- import java.util.*;
- import java.util.Arrays;
- public class P1V00892515 {
- public static void main(String[] args) {
- //Main Method for testing and printing output
- Scanner sc = new Scanner(System.in);
- int n = sc.nextInt();
- int[] array = new int[n];
- sc.nextLine();
- String[] nums = sc.nextLine().split(" ");
- for (int i = 0; i < n; i++) {
- array[i] = Integer.parseInt(nums[i]);
- }
- Arrays.sort(array);
- //int[] sortedarr= selSort(array);
- int[] cftrue = marvel(array);
- System.out.println(cftrue[0]+" "+ cftrue[1]);
- }
- public static int[] marvel(int[] arr) {
- //Finds the closest pair in the sorting list
- int[] cf = {(arr[0]), (arr[arr.length-1])};
- int globalcf = (arr[arr.length-1] - arr[0]);
- for(int i=(arr.length-1);i>0;i--){
- if(globalcf > (arr[i]-arr[i-1])) {
- cf[0]=arr[i-1];
- cf[1]=arr[i];
- globalcf=(arr[i]-arr[i-1]);
- }
- if(globalcf==0) {
- return(cf);
- }
- }
- return cf;
- }
- //Removed Selection sort algorithm because I opted for the arrays.sort java function.
- /*
- public static int[] selSort(int[] arr) {
- //Selection sort algorithm helper method
- int temp;
- for(int i=0; i<arr.length -1; i++) {
- int min = 0;
- for(int j=i+1; j<arr.length; j++) {
- if(arr[j] < arr[i]) {
- min= arr[j];
- temp = arr[i];
- arr[i]=arr[j];
- arr[j]=temp;
- }
- }
- }
- return arr;
- }
- */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement