How would I change my project so that the searching and sorting methods use an array list instead of an integer array? **MAIN CLASS** public
How would I change my project so that the searching and sorting methods use an array list instead of an integer array?
**MAIN CLASS**
public class Main {
public static void main(String[] args) { int[] numbers = {1,3,5,8,12}; int result = Searching.linearSearch(numbers, 5); System.out.println(result); }
}
**SORTING CLASS**
public class Sorting { public static void selectionSort(int[] numbers) { int i = 0; int j = 0; int indexSmallest = 0; int temp = 0; // Temporary variable for swap
for (i = 0; i < numbers.length; ++i) {
// Find index of smallest remaining element indexSmallest = i; for (j = i + 1; j < numbers.length; ++j) {
if (numbers[j] < numbers[indexSmallest]) { indexSmallest = j; } }
// Swap numbers[i] and numbers[indexSmallest] temp = numbers[i]; numbers[i] = numbers[indexSmallest]; numbers[indexSmallest] = temp; } } public static void insertionSort(int[] numbers) { int i = 0; int j = 0; int temp = 0; // Temporary variable for swap
for (i = 1; i < numbers.length; ++i) { j = i; // Insert numbers[i] into sorted part // stopping once numbers[i] in correct position while (j > 0 && numbers[j] < numbers[j - 1]) {
// Swap numbers[j] and numbers[j - 1] temp = numbers[j]; numbers[j] = numbers[j - 1]; numbers[j - 1] = temp; --j; } } } public static void mergeSort(int[] numbers) { MergeSort.mergesort(numbers, 0, numbers.length - 1); } public static void quickSort(int[] numbers) { QuickSort.quicksort(numbers, 0, numbers.length - 1); } }
**SEARCHING CLASS**
public class Searching { public static int linearSearch(int numbers[], int key) { int i = 0; for (i = 0; i < numbers.length; ++i) { if (numbers[i] == key) { return i; /* position */ } } return -1; /* not found */ }
public static int binarySearch(int numbers[], int key) { int mid = 0; int low = 0; int high = 0;
high = numbers.length - 1;
while (high >= low) { mid = (high + low) / 2; if (numbers[mid] < key) { low = mid + 1; } else if (numbers[mid] > key) { high = mid - 1; } else { return mid; } } return -1; // not found } }
**QuickSort class**
public class QuickSort { public static int partition(int numbers[], int i, int k) { int l = 0; int h = 0; int midpoint = 0; int pivot = 0; int temp = 0; boolean done = false;
/* Pick middle element as pivot */ midpoint = i + (k - i) / 2; pivot = numbers[midpoint];
l = i; h = k;
while (!done) {
/* Increment l while numbers[l] < pivot */ while (numbers[l] < pivot) { ++l; }
/* Decrement h while pivot < numbers[h] */ while (pivot < numbers[h]) { --h; }
/* * If there are zero or one items remaining, all numbers are partitioned. Return * h */ if (l >= h) { done = true; } else { /* * Swap numbers[l] and numbers[h], update l and h */ temp = numbers[l]; numbers[l] = numbers[h]; numbers[h] = temp;
++l; --h; } } return h; }
public static void quicksort(int numbers[], int i, int k) { int j = 0;
/* * Base case: If there are 1 or zero entries to sort, partition is already * sorted */ if (i >= k) { return; }
/* * Partition the data within the array. Value j returned from partitioning is * location of last item in low partition. */ j = partition(numbers, i, k);
/* * Recursively sort low partition (i to j) and high partition (j + 1 to k) */ quicksort(numbers, i, j); quicksort(numbers, j + 1, k);
return; }
}
**MergeSort**
public class MergeSort { public static void merge(int numbers[], int i, int j, int k) { int mergedSize = k - i + 1; // Size of merged partition int mergedNumbers[] = new int[mergedSize]; // Temporary array for merged numbers int mergePos = 0; // Position to insert merged number int leftPos = 0; // Position of elements in left partition int rightPos = 0; // Position of elements in right partition
leftPos = i; // Initialize left partition position rightPos = j + 1; // Initialize right partition position
// Add smallest element from left or right partition to merged numbers while (leftPos <= j && rightPos <= k) { if (numbers[leftPos] < numbers[rightPos]) { mergedNumbers[mergePos] = numbers[leftPos]; ++leftPos; } else { mergedNumbers[mergePos] = numbers[rightPos]; ++rightPos; } ++mergePos; }
// If left partition is not empty, add remaining elements to merged numbers while (leftPos <= j) { mergedNumbers[mergePos] = numbers[leftPos]; ++leftPos; ++mergePos; }
// If right partition is not empty, add remaining elements to merged numbers while (rightPos <= k) { mergedNumbers[mergePos] = numbers[rightPos]; ++rightPos; ++mergePos; }
// Copy merge number back to numbers for (mergePos = 0; mergePos < mergedSize; ++mergePos) { numbers[i + mergePos] = mergedNumbers[mergePos]; } }
public static void mergesort(int numbers[], int i, int k) { int j = 0;
if (i < k) { j = (i + k) / 2; // Find the midpoint in the partition
// Recursively sort left and right partitions mergesort(numbers, i, j); mergesort(numbers, j + 1, k);
// Merge left and right partition in sorted order merge(numbers, i, j, k); } }
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started