50 algorithmic exercises in JAVA 50 Practical Exercises to Develop Your Programming Programming Skills (Dave, John) (Z-Library)
JavaAuthor:Dave, John
No description
Tags
Support Statistics
¥.00 ·
0times
Text Preview (First 20 pages)
Registered users can read the full content for free
Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.
Page
1
(This page has no text content)
Page
2
50 ALGORITHMIC EXERCISES IN JAVA
Page
3
INTRODUCTION Algorithms are at the heart of computer science. It is the art of solving problems using sequences of logical instructions. Whether you are a beginner looking to master the basics of programming or an experienced developer looking to improve your problem-solving skills, this book is designed for you.
Page
4
STATEMENT: BUBBLE SORTING IN JAVA Write a Java program to sort an array of integers using the bubble sort algorithm. The bubble sort algorithm works by comparing and swapping adjacent elements if they are in the wrong order, and repeats this process until the whole array is sorted. You need to code the bubble sort logic in Java and make sure that your program works correctly. You can also add comments to explain your code. Example of a solution in Java : import java.util.Scanner; public class TriBubbles { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter the size of the table: "); int n = scanner.nextInt(); int[] array = new int[n]; System.out.println("Enter the elements of the table:"); for (int i = 0; i < n; i++) { array[i] = scanner.nextInt(); } // Implementation of the bubble sorting algorithm
Page
5
for (int i = 0; i < n - 1; i++) { for (int j = 0; j < n - 1 - i; j++) { if (array[j] > array[j + 1]) { int temp = array[j]; array[j] = array[j + 1]; array[j + 1] = temp; } } } System.out.println("Table sorted:"); for (int i = 0; i < n; i++) { System.out.print(array[i] + " "); } } } In this example, we have used an array of integers to store the elements, and the bubble sort algorithm is implemented inside two for loops. The program asks the user to enter the size of the array and the elements of the array, then displays the sorted array at the end.
Page
6
STATEMENT: QUICKSORT IN JAVA Write a Java program to sort an array of integers using the quicksort algorithm. The quicksort algorithm is an efficient sorting algorithm based on the divide-and-conquer method. It selects a pivot element, partitions the array into two sub-arrays (elements below the pivot and elements above the pivot), then recursively sorts these sub-arrays. You need to code the quick sort logic in Java and make sure your program works correctly. You can also add comments to explain your code. Example of a solution in Java : import java.util.Scanner; public class QuickSort { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter the size of the table: "); int n = scanner.nextInt(); int[] array = new int[n]; System.out.println("Enter the elements of the table:"); for (int i = 0; i < n; i++) { array[i] = scanner.nextInt(); }
Page
7
// Call up the quick sort function quickSort(array, 0, n - 1); System.out.println("Table sorted:"); for (int i = 0; i < n; i++) { System.out.print(array[i] + " "); } } public static void quickSort(int[] arr, int low, int high) { if (low < high) { int pivotIndex = partition(arr, low, high); quickSort(arr, low, pivotIndex - 1); quickSort(arr, pivotIndex + 1, high); } } public static int partition(int[] arr, int low, int high) { int pivot = arr[high]; int i = low - 1; for (int j = low; j < high; j++) { if (arr[j] < pivot) { i++; int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp;
Page
8
} } int temp = arr[i + 1]; arr[i + 1] = arr[high]; arr[high] = temp; return i + 1; } } In this example, we have used the quicksort method to sort an array of integers. The program asks the user to enter the size of the array and the elements, then uses recursion to sort the array.
Page
9
STATEMENT: MERGESORT IN JAVA Write a Java program to sort an array of integers using the merge sort algorithm (Mergesort). The merge sort algorithm is based on the "divide and conquer" method by dividing the array into two halves, sorting these halves, then merging the sorted halves to obtain a sorted array. You need to code the fusion sorting logic in Java and make sure that your program works correctly. You can also add comments to explain your code. Example of a solution in Java : import java.util.Scanner; public class MergeSort { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter the size of the table: "); int n = scanner.nextInt(); int[] array = new int[n]; System.out.println("Enter the elements of the table:"); for (int i = 0; i < n; i++) { array[i] = scanner.nextInt(); }
Page
10
// Call the merge sort function mergeSort(array, 0, n - 1); System.out.println("Table sorted:"); for (int i = 0; i < n; i++) { System.out.print(array[i] + " "); } } public static void mergeSort(int[] arr, int left, int right) { if (left < right) { int middle = (left + right) / 2; mergeSort(arr, left, middle); mergeSort(arr, middle + 1, right); merge(arr, left, middle, right); } } public static void merge(int[] arr, int left, int middle, int right) { int n1 = middle - left + 1; int n2 = right - middle; int[] leftArray = new int[n1]; int[] rightArray = new int[n2]; for (int i = 0; i < n1; i++) { leftArray[i] = arr[left + i]; }
Page
11
for (int j = 0; j < n2; j++) { rightArray[j] = arr[middle + 1 + j]; } int i = 0, j = 0, k = left; while (i < n1 && j < n2) { if (leftArray[i] <= rightArray[j]) { arr[k] = leftArray[i]; i++; } else { arr[k] = rightArray[j]; j++; } k++; } while (i < n1) { arr[k] = leftArray[i]; i++; k++; } while (j < n2) { arr[k] = rightArray[j]; j++; k++;
Page
12
} } } In this example, we have used the Mergesort algorithm to sort an array of integers. The program asks the user to enter the size of the array and the elements, then uses recursion to sort the array by dividing, sorting and merging the sub-arrays.
Page
13
STATEMENT: SORTING BY SELECTION IN JAVA Write a Java program to sort an array of integers using the selection sort algorithm. The selection sort algorithm consists of finding the smallest element in the array and placing it in the first position, then repeating this process for the rest of the array. You need to code the sort-by-selection logic in Java and make sure that your program works correctly. You can also add comments to explain your code. Example of a solution in Java : import java.util.Scanner; public class SelectionSort { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter the size of the table: "); int n = scanner.nextInt(); int[] array = new int[n]; System.out.println("Enter the elements of the table:"); for (int i = 0; i < n; i++) { array[i] = scanner.nextInt(); } // Call up the sort by selection function
Page
14
selectionSort(array); System.out.println("Table sorted:"); for (int i = 0; i < n; i++) { System.out.print(array[i] + " "); } } public static void selectionSort(int[] arr) { int n = arr.length; for (int i = 0; i < n - 1; i++) { int minIndex = i; for (int j = i + 1; j < n; j++) { if (arr[j] < arr[minIndex]) { minIndex = j; } } int temp = arr[i]; arr[i] = arr[minIndex]; arr[minIndex] = temp; } } } In this example, we have used the selection sort algorithm to sort an array of integers. The program asks the user to enter the size of the array and the elements, then uses a double loop to find the minimum element in the
Page
15
unsorted portion of the array and places it in the correct position. This operation is repeated until the entire array is sorted.
Page
16
STATEMENT: INSERTION SORTING IN JAVA Write a Java program to sort an array of integers using the insertion sort algorithm. The insertion sort algorithm consists of successively inserting each unsorted element in its correct position among the sorted elements, from left to right. You need to code the insertion sort logic in Java and make sure that your program works correctly. You can also add comments to explain your code. Example of a solution in Java : import java.util.Scanner; public class InsertionSort { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter the size of the table: "); int n = scanner.nextInt(); int[] array = new int[n]; System.out.println("Enter the elements of the table:"); for (int i = 0; i < n; i++) { array[i] = scanner.nextInt(); } // Call the insertion sort function insertSort(array);
Page
17
System.out.println("Table sorted:"); for (int i = 0; i < n; i++) { System.out.print(array[i] + " "); } } public static void insertionSort(int[] arr) { int n = arr.length; for (int i = 1; i < n; i++) { int key = arr[i]; int j = i - 1; while (j >= 0 && arr[j] > key) { arr[j + 1] = arr[j]; j--; } arr[j + 1] = key; } } } In this example, we have used the insertion sort algorithm to sort an array of integers. The program asks the user to enter the size of the array and the elements, then uses a loop to insert each unsorted element in its correct position among the sorted elements. This operation is repeated until the entire array is sorted.
Page
18
STATEMENT: BINARY SEARCH IN JAVA Write a Java program that performs a binary search in a sorted array of integers. Binary search is an efficient algorithm that searches for an element in a sorted array by recursively dividing the array into two parts and comparing the element searched for with the element in the middle. If the element searched for is equal to the element in the middle, the algorithm returns the position of this element in the array. Otherwise, the algorithm reduces the search to the half of the array in which the element could be found. You need to code the binary search logic in Java and make sure that your program works correctly. You can also add comments to explain your code. Example of a solution in Java : import java.util.Scanner; public class BinarySearch { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter the size of the sorted array: "); int n = scanner.nextInt(); int[] array = new int[n]; System.out.println("Enter the elements of the sorted array (in ascending order):"); for (int i = 0; i < n; i++) {
Page
19
array[i] = scanner.nextInt(); } System.out.print("Enter the item to search for: "); int elementSearch = scanner.nextInt(); int position = binarySearch(array, elementSearch); if (position != -1) { System.out.println("The element " + elementSearch + " has been found at position " + position + " in the array."); } else { System.out.println("The element " + elementSearch + " was not found in the array."); } } public static int binarySearch(int[] arr, int target) { int left = 0; int right = arr.length - 1; while (left <= right) { int mid = left + (right - left) / 2; if (arr[mid] == target) { return mid; } if (arr[mid] < target) { left = mid + 1; } else {
Page
20
right = mid - 1; } } return -1; } } In this example, we have used the binary search algorithm to find an element in a sorted array. The program asks the user to enter the size of the sorted array, the elements of the array and the element to be searched for. It then performs the binary search to find the element and displays its position if it is found, or indicates if it has not been found.
Comments 0
Loading comments...
Reply to Comment
Edit Comment