static void Sort (int[] A, int size) {
//
int i, j;
for (i=1; i<size; i++) {
// All elements in A[1]..A[i] are
less than
// all elements in A[i+1]..A[size],
and
// a[1]..A[i] is sorted.
int min = findMinimum(A, i, size);
swap(A, i, min);
}
}
static int findMinimum (int[] A, int i, int size) {
// find and return the index of the smallest element in
A[i..size]
int j, min = i;
for (j=i+1; j<=size; j++)
// A[min] <= all elements in
A[i]..A[j-1]
if (A[j] < A[min]) min = j;
return min;
}
static void swap (int[] A, int i, int j) {
// exchange (swap) the ith and jth elements of A
int temp = A[i];
A[i] = A[j];
A[j] = temp;
}
The sorting process works as a series of passes on the array A.
In each pass, the smallest element of a subarray, from position i to the
end of A, is located and then swapped (exchanged) with the first element
of that subarray. There is one pass for each value of i from 1 to
size-1. Here is an example, with a 5-element array A (size = 5):
These three methods can be adapted so that they sort and display
the arrays words and counts in alphabetical order. Note that whenever
a pair of words is swapped inside the words array, the corresponding counts
in the counts array must be swapped as well. That is, each word must
remain "in parallel" with its frequency count. We return to this
problem after a brief introduction to character
"Enter file name: "
A String variable is any variable declared with the type String. The following are String variables:
String s, t, u;
The value of a String variable can be any String constant, including the empty String "" (the one that has no characters between the quotes).
The String class has a number of methods that allow programs to manipulate String values in different ways. The discussion below illustrates how some of these methods are used by showing some simple examples.
s = "Hello World!";
The length of s is 12, and the method length returns the length of any String. Thus, the expression s.length() returns 12 in this example.
The ith character of a String variable can be isolated by using the charAt method. String values can be viewed as arrays of characters in this sense, so the expression s.charAt(0) returns the first character 'H', s.charAt(1) returns the second character 'e', and so forth. [Note that single characters are enclosed in single quotes (') rather than double quotes ("), which enclose strings. Thus the character H is written as 'H', while the 1-character String H is written as "H".]
Two String values can be concatenated (joined together) to form a single String, either using the + sign or using the concat method, which is part of the String class. Either of the following statements leaves the variable u with the value "Hello World! Hello!".
u = s + " Hello!";
u = s.concat(" Hello!");
Two String values can be compared for equality or nonequality using the equals method. Equality for String values means that one is an identical copy of the other, letter for letter. So we might write
if (s.equals("Hello!")) ...
to test whether or not the current value of s is identical to the String "Hello!". A related method is called equalsIgnoreCase, which will be true if the two strings are identical except for capitalization differences. For instance, if s=="Hello", then s.equals("heLLO") is false but s.equalsIgnoreCase("heLLO") is true.
To test whether one string precedes another in an alphabetical sense, the compareTo method can be used. The expression s.compareTo("hello") returns a number < 0, 0, or > 0 depending on whether the value of s precedes, matches, or follows the string "Hello" alphabetically. So, considering an array 'words' of strings, the following if statement would test to see if string words[i] follows words[j] in the dictionary, and if so swap them:
if (words[i].compareTo(words[j]) > 0))
swap(words, i, j);
This example assumes, of course, that we have a method swap that will exchange the values of two entries in an array of strings.
t = s.substring(6, 11);
Here, the 6 designates the starting position of the substring to be copied, and the 11 designates the position of the next character in s following the substring (counting the first character at position 0, not 1). Note that the value of s itself is not affected by this action.
If we wanted to delete that same substring from s, we would need to concatenate the beginning part of s with that part which follows that substring. That is, we would write
s = s.substring(0, 5) + s.substring(11);
to obtain the string "Hello" and assign it to s. Note here that the expression s.substring(11) takes that substring which begins at position 11 and ends at the end of s. That is, it is the entire righthand end of s.
Finally, to insert a new substring within an existing String, we use concatenation and substring in a different way. For instance, to insert the word "Cruel" inside the string s = "Hello World!" to form s = "Hello Cruel World!" we would write the following assignment:
s = s.substring(0, 5) + " Cruel " + s.substring(6);
i = s.indexOf("Cruel");
s = s.replace(s.charAt(i), 'c');
The resulting value of i in this example is 6 (the position of the first character of the leftmost occurrence of the substring "Cruel" within s). The resulting value of s is "Hello cruel World!" since s.charAt(i) returns the character 'C'. Here are some simple exercises to check your understanding of the above ideas.