-
Notifications
You must be signed in to change notification settings - Fork 82
/
ArrayExamples.java
149 lines (119 loc) · 3.55 KB
/
ArrayExamples.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
//Mike Scott
//examples of array manipulations
public class ArrayExamples
{ public static void main(String[] args)
{ int[] list = {1, 2, 3, 4, 1, 2, 3};
findAndPrintPairs(list, 5);
bubblesort(list);
showList(list);
list = new int[]{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
bubblesort(list);
showList(list);
list = new int[]{11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, -1, -2};
bubblesort(list);
showList(list);
list = new int[]{1};
bubblesort(list);
showList(list);
}
// pre: list != null, list.length > 0
// post: return index of minimum element of array
public static int findMin(int[] list)
{ assert list != null && list.length > 0 : "failed precondition";
int indexOfMin = 0;
for(int i = 1; i < list.length; i++)
{ if(list[i] < list[indexOfMin])
{ indexOfMin = i;
}
}
return indexOfMin;
}
/*
*pre: list != null, newSize >= 0
*post: nothing. the method does not succeed it resizing the
* argument
*/
public static void badResize(int[] list, int newSize)
{ assert list != null && newSize >= 0 : "failed precondition";
int[] temp = new int[newSize];
int limit = Math.min(list.length, newSize);
for(int i = 0; i < limit; i++)
{ temp[i] = list[i];
}
// uh oh!! Changing pointer, not pointee. This breaks the
// relationship between the parameter and argument
list = temp;
}
/*
*pre: list != null, newSize >= 0
*post: returns an array of size newSize. Elements from 0 to newSize - 1
* will be copied into the new array
*/
public static int[] goodResize(int[] list, int newSize)
{ assert list != null && newSize >= 0 : "failed precondition";
int[] result = new int[newSize];
int limit = Math.min(list.length, newSize);
for(int i = 0; i < limit; i++)
{ result[i] = list[i];
}
return result;
}
/*
*pre: list != null
*post: prints out the indices and values of all pairs of numbers
*in list such that list[a] + list[b] = target
*/
public static void findAndPrintPairs(int[] list, int target)
{ assert list != null : "failed precondition";
for(int i = 0; i < list.length; i++)
{ for(int j = i + 1; j < list.length; j++)
{ if(list[i] + list[j] == target)
{ System.out.println("The two elements at indices " + i + " and " + j
+ " are " + list[i] + " and " + list[j] + " add up to " + target);
}
}
}
}
/*
*pre: list != null;
*post: sort the elements of list so that they are in ascending order
*/
public static void bubblesort(int[] list)
{
assert list != null : "failed precondition";
int temp;
boolean changed = true;
for(int i = 0; i < list.length && changed; i++)
{ changed = false;
for(int j = 0; j < list.length - i - 1; j++)
{ assert (j > 0) && (j + 1 < list.length) : "loop counter j " + j +
"is out of bounds.";
if(list[j] > list[j+1])
{ changed = true;
temp = list[j + 1];
list[j + 1] = list[j];
list[j] = temp;
}
}
}
assert isAscending( list );
}
public static void showList(int[] list)
{ for(int i = 0; i < list.length; i++)
System.out.print( list[i] + " " );
System.out.println();
}
/* pre: list != null
post: return true if list is sorted in ascedning order, false otherwise
*/
public static boolean isAscending( int[] list )
{ boolean ascending = true;
int index = 1;
while( ascending && index < list.length )
{ assert index >= 0 && index < list.length;
ascending = (list[index - 1] <= list[index]);
index++;
}
return ascending;
}
}