Task: In the array, all elements are different. Swap the minimum and maximum element of this array. Lead two ways to solve.

The first way I did. Source:

package chetirnadcat; import java.util.Scanner; class Chetirnadcat2 { public static void main(String[] args) { System.out.println("Π’Π²Π΅Π΄ΠΈΡ‚Π΅ ΠΊΠΎΠ»-Π²ΠΎ элСмСнтов"); Scanner in = new Scanner(System.in); int b = in.nextInt(); int[] a = new int[b]; int i; int r; int m; int k = 0; int l = 0; boolean z = false; for (i = 0; i < b; i++) { System.out.println("Π’Π²Π΅Π΄ΠΈΡ‚Π΅ элСмСнт массива ΠΏΠΎΠ΄ Π½ΠΎΠΌΠ΅Ρ€ΠΎΠΌ " + i); a[i] = in.nextInt(); } for (i = 0; i < (b - 1); i++) { r = a[i]; for (i = 1; i < b; i++) { if (r == a[i]) { System.out.println("ВсС элСмСнты Π΄ΠΎΠ»ΠΆΠ½Ρ‹ Π±Ρ‹Ρ‚ΡŒ Ρ€Π°Π·Π½Ρ‹ΠΌΠΈ"); z = true; } } } if (z == false) { int min = a[0]; int max = a[0]; for (i = 0; i < b; i++) { if (a[i] < min) { min = a[i]; l = l + 1; } } for (i = 0; i < b; i++) { if (a[i] > max) { max = a[i]; k = k + 1; } } System.out.println("ΠœΠ°ΠΊΡΠΈΠΌΠ°Π»ΡŒΠ½Ρ‹ΠΉ элСмСнт массива = " + max); System.out.println("ΠœΠΈΠ½ΠΈΠΌΠ°Π»ΡŒΠ½Ρ‹ΠΉ элСмСнт массива = " + min); for (i = 0; i < b; i++) { System.out.print(a[i] + " "); } /*ΠŸΠ•Π Π’Π«Π™ Π‘ΠŸΠžΠ‘ΠžΠ‘*/ m = a[l]; a[l] = a[k]; a[k] = m; System.out.println(" "); for (i = 0; i < b; i++) { System.out.print(a[i] + " "); } } } } 

How to make the second way?

  • and what the second should be? based on what? - Alexey Shimansky
  • I have no idea, but they require two methods ( - Marat Zimnurov
  • 2
    Do not save on spaces, line breaks and neglect tabs. - Regent
  • We will know. With the second method, tell me? @Regent - Marat Zimnurov
  • @MaratZimnurov You and the first method is not correct. :) However, the second method may be to sort the array. - Vlad from Moscow

3 answers 3

Radically different search algorithms min. and max. elements with adequate computing power, I did not find. You can, of course, pre-sort the array in order to take the first and last elements, but this is not even O(n) .

The first algorithm consists in passing through the array and comparing each element with the minimum and maximum, which were selected at the beginning of the first option. This is the algorithm that you implemented.

The second algorithm operates according to a similar scheme, but the elements are pre-divided into pairs and sorted within a pair in ascending order. After that, passing through the modified array in the search for the minimum and maximum occurs, however, a pair of elements is considered at once, and only the first element of the pair is compared with the minimum, and the second is compared with the maximum.

 public static void main(String[] args) { System.out.println("Π’Π²Π΅Π΄ΠΈΡ‚Π΅ ΠΊΠΎΠ»-Π²ΠΎ элСмСнтов"); Scanner in = new Scanner(System.in); int elementsCount = in.nextInt(); int[] elements = new int[elementsCount]; for (int i = 0; i < elementsCount; i++) { System.out.println("Π’Π²Π΅Π΄ΠΈΡ‚Π΅ элСмСнт массива ΠΏΠΎΠ΄ Π½ΠΎΠΌΠ΅Ρ€ΠΎΠΌ " + i); elements[i] = in.nextInt(); } for (int i = 0; i < elementsCount; i++) { int firstElement = elements[i]; for (int j = i + 1; j < elementsCount; j++) { int secondElement = elements[j]; if (firstElement == secondElement) { System.out.println("ВсС элСмСнты Π΄ΠΎΠ»ΠΆΠ½Ρ‹ Π±Ρ‹Ρ‚ΡŒ Ρ€Π°Π·Π½Ρ‹ΠΌΠΈ"); return; } } } printArray(elements); firstAlgorithm(elements); secondAlgorithm(elements); } private static void firstAlgorithm(int[] data) { int[] elements = new int[data.length]; System.arraycopy(data, 0, elements, 0, data.length); int minIndex = 0; int min = elements[minIndex]; int maxIndex = 0; int max = elements[maxIndex]; for (int i = 1; i < elements.length; i++) { if (elements[i] < min) { min = elements[i]; minIndex = i; } else if (elements[i] > max) { max = elements[i]; maxIndex = i; } } System.out.println("ΠŸΠ΅Ρ€Π²Ρ‹ΠΉ способ:"); System.out.println("ΠœΠ°ΠΊΡΠΈΠΌΠ°Π»ΡŒΠ½Ρ‹ΠΉ элСмСнт массива = " + max); System.out.println("ΠœΠΈΠ½ΠΈΠΌΠ°Π»ΡŒΠ½Ρ‹ΠΉ элСмСнт массива = " + min); swapElements(elements, minIndex, maxIndex); printArray(elements); } private static void secondAlgorithm(int[] data) { int[] elements = new int[data.length]; System.arraycopy(data, 0, elements, 0, data.length); int[] elementsPairs = new int[data.length]; System.arraycopy(data, 0, elementsPairs, 0, data.length); if (data.length == 1) { System.out.println("Π’Ρ‚ΠΎΡ€ΠΎΠΉ способ: Π² массивС всСго ΠΎΠ΄ΠΈΠ½ элСмСнт"); return; } HashMap<Integer, Integer> swappedPlaceToRealPlace = new HashMap<>(); for (int i = 0; i < elementsPairs.length; i += 2) { if (i + 1 < elementsPairs.length && elementsPairs[i] > elementsPairs[i + 1]) { swapElements(elementsPairs, i, i + 1); swappedPlaceToRealPlace.put(i, i + 1); swappedPlaceToRealPlace.put(i + 1, i); } } int minIndex = 0; int min = elementsPairs[minIndex]; int maxIndex = 1; int max = elementsPairs[maxIndex]; for (int i = 2; i < elementsPairs.length; i += 2) { int firstIndex = i; int secondIndex = i + 1; if (secondIndex == elementsPairs.length) { secondIndex = i; } if (elementsPairs[firstIndex] < min) { min = elementsPairs[firstIndex]; minIndex = i; } else if (elementsPairs[secondIndex] > max) { max = elementsPairs[secondIndex]; maxIndex = secondIndex; } } if (swappedPlaceToRealPlace.containsKey(minIndex)) { minIndex = swappedPlaceToRealPlace.get(minIndex); } if (swappedPlaceToRealPlace.containsKey(maxIndex)) { maxIndex = swappedPlaceToRealPlace.get(maxIndex); } System.out.println("Π’Ρ‚ΠΎΡ€ΠΎΠΉ способ:"); System.out.println("ΠœΠ°ΠΊΡΠΈΠΌΠ°Π»ΡŒΠ½Ρ‹ΠΉ элСмСнт массива = " + max); System.out.println("ΠœΠΈΠ½ΠΈΠΌΠ°Π»ΡŒΠ½Ρ‹ΠΉ элСмСнт массива = " + min); swapElements(elements, minIndex, maxIndex); printArray(elements); } private static void swapElements(int[] array, int i, int j) { int swap = array[i]; array[i] = array[j]; array[j] = swap; } private static void printArray(int[] array) { for (int i = 0; i < array.length; i++) { System.out.print(array[i] + " "); } System.out.println(); } 

It turned out a lot of code. If the elements did not need to be rearranged in places, then it would be significantly less.

In the first algorithm (the firstAlgorithm method), I think everything is clear. In the second (the secondAlgorithm method) elementPairs contains a modified array, in which the sorting is performed within pairs. swappedPlaceToRealPlace used to restore min indexes. and max. elements in the source ( elements ) array after they are in the modified ( elementsPairs ) array. This is necessary, again, only for rearranging elements in places.

From the point of view of theory, both algorithms work in linear time O(n) , however, it is assumed that in practice the second should work somewhat faster due to the smaller number of operations. It is possible that due to the add. costs associated with the creation of additional. copies of the array and saving the indices in the original array, it will not work faster.

The second algorithm is taken from here .

  • Thank you I see it was interesting to you) - Marat Zimnurov

I do not quite understand why you are looking for a minimum and maximum in two different cycles:

  for (i = 0; i < b; i++) { if (a[i] < min) { min = a[i]; l = l + 1; } } for (i = 0; i < b; i++) { if (a[i] > max) { max = a[i]; k = k + 1; } } 

You can find a minimum and a maximum in one cycle - isn't it right? This can be given for the second way :)

Update

For example:

 min=Integer.MAX_VALUE; max=Integer.MIN_VALUE; for (i = 0; i < b; i++) { if (a[i] < min) min = a[i]; if(a[i] > max) max = a[i]; } 
  • Yes, I have a compiler mowing the code first, because I divided everything to see where the joint was) - Marat Zimnurov
 public class App { public static void main(String[ ] args) { int[] a = {1,5,3,2,6}; int nMin = 0; int nMax = 0; int max = a[0]; int min = a[0]; for (int i = 0; i < a.length; i++) { if (a[i] > max) { max = a[i]; nMax = i; } if (a[i] < min) { min = i; nMin = i; } } int tmp = a[nMax]; a[nMax] = a[nMin]; a[nMin] = tmp; for (int item: a) { System.out.print(item + ","); } }