-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathL7Q3_missingElement.java
78 lines (72 loc) · 2.09 KB
/
L7Q3_missingElement.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/***
* 03-04-2020
*
* Lab 7: Question 3
*
* Description:
* Given an of numbers, use Bubble sort to sort the array. The array contains
* a group of consecutive numbers, however, one number is missing. Once the
* array is sorted, find and print the missing number. You may assume all numbers
* are between the first sorted element and the last sorted element. The code for
* the array is given to you, please DO NOT CHANGE THIS.
*
* Sample Arrays 1: {1, 4, 7, 9, 3, 6, 8, 5}
*
* Sample Output 1: 2
*
*
* Algorithm:
*
* Step 1: Sort the array --> bSort() method
*
* Step 2: Determine the missing element in the sequence --> missE() method
* Since we know the array contains a group of consecutive numbers,
* we can find the missing value by iterating through the sequence
* and comparing adjacent values as in the bubblesort method.
*
* If for a given pair, i and j, where i is exactly onde index position
* less than j, we find that j != i + 1, then j is not the next conecutive
* number and we have found the missing value in the sequence.
*
* Step 3: Print the missing number: x = i + 1, where x is the missing number and i is the number preceding x.
*
***/
public class L7Q3_missingElement
{
public static void main(String[] args)
{
//DO NOT CHANGE THE ARRAY DECLARATION
int[] missing = {1, 4, 7, 9, 3, 6, 8, 5};
bSort(missing);
missE(missing);
} //End main()
public static void bSort(int[] missing)
{ //Step 3:
int t = 0;
boolean isSorted = true;
for(int i = 1; i < missing.length; i++)
{
for(int j = 0; j < missing.length - i; j++)
{
if(missing[j] > missing[j + 1])
{
t = missing[j];
missing[j] = missing[j + 1];
missing[j + 1] = t;
isSorted = false;
}
} //End inner-loop
if(isSorted) return;
} //End outer-loop
} //End bSort()
public static void missE(int[] missing)
{
for(int i = 0; i < missing.length - 1; i++)
{
if(missing[i + 1] != (missing[i] + 1))
{
System.out.println(missing[i] + 1); break;
}
}
} //End missE()
} //End calss