Labor – Char-Array: Sorting + Shuffling + more

CharArrHandlingchars: char[]freeIdx: intConstructor(capacity: int, txt: String)replaceArray(newArr: char[], newFreeIdx: int)selectionSort()bubbleSort()shuffle()invert()subArray(int startIdx, int endIdxExcl): char[]toString(): StringisArrSorted(chars: char[], ascending: boolean): boolean swap(idx1, idx2)printArray(chArr: char[], freeIdx: int, title: String, cols: int)
  • Constructor(..) …​ transforms given String to char-Array. If capacity too small, use only the fitting part of txt. Set freeIdx accordingly.

  • replaceArray(..) …​ replace the attribute with the given array.

  • selectionSort() …​ sorts the (valid part of) chars-Array with Selectionsort (using helper method findMinValPos(..) and swap(..))

  • bubbleSort() …​ sorts the (valid part of) chars-Array with Bubblesort (using helper method oneBubble(..) and swap(..)!)

  • shuffle() …​ mix the chars by going through the array and swapping with a randomly defined other position. Use swap(..) for that!

  • invert() …​ swap first with last, second with one before last, etc. (use swap(..))

  • subArray(..) …​ create a new array of exactly needed length (calculated from startIdx and endIdxExcl) and fill it with the proper values from the Array attribute chars. If both indices are outside valid range (freeIdx!), return array of length 0. If endIdxExcl outside, use freeIdx, if startIdx outside, use 0 instead. (usable e.g. to cut out a specific word by finding the spaces around it)

  • toString …​ create a String from the array (without separating elements)

  • isArrSorted(..) …​ checks if array is sorted by checking if every pair of neighbors hs the right relation

  • swap(..) …​ exchange the values at the given indices

  • printArray(..) …​ static method to print given array (up to the given freeIdx), a title, cols (= elements per row). The title is printed above to give a hint what operation was done/what is the state with the array.