0% found this document useful (0 votes)
14 views7 pages

ICSE Array Search Sort Programs

The document contains Java code examples for various array operations including creating, displaying, sorting, searching, and modifying arrays. Each example includes a brief explanation of the operation performed, such as finding the sum, average, largest and smallest elements, and handling duplicates. The document serves as a comprehensive guide for basic array manipulations in Java.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views7 pages

ICSE Array Search Sort Programs

The document contains Java code examples for various array operations including creating, displaying, sorting, searching, and modifying arrays. Each example includes a brief explanation of the operation performed, such as finding the sum, average, largest and smallest elements, and handling duplicates. The document serves as a comprehensive guide for basic array manipulations in Java.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1.

Create and display an array of integers


Explanation: This program initializes an array with 5 integers and displays
them using a for loop.
Code:
import [Link].*;
public class CreateArray {
public static void main(String[] args) {
int[] arr = {10, 20, 30, 40, 50};
[Link]("Array elements:");
for (int i = 0; i < [Link]; i++) {
[Link](arr[i] + " ");
}
}
}
2. Find the sum and average of elements in an array
Explanation: This program calculates the sum of array elements and then com
putes the average.
Code:
import [Link].*;
public class SumAverage {
public static void main(String[] args) {
int[] arr = {10, 20, 30, 40, 50};
int sum = 0;
for (int num : arr) {
sum += num;
}
double avg = (double) sum / [Link];
[Link]("Sum: " + sum);
[Link]("Average: " + avg);
}
}
3. Find the largest and smallest element in an array
Explanation: This program iterates through the array to find the largest an
d smallest values.
Code:
public class LargestSmallest {
public static void main(String[] args) {
int[] arr = {10, 20, 5, 40, 30};
int largest = arr[0];
int smallest = arr[0];
for (int num : arr) {
if (num > largest) largest = num;
if (num < smallest) smallest = num;
}
[Link]("Largest: " + largest);
[Link]("Smallest: " + smallest);
}
}
4. Count even and odd numbers in an array
Explanation: Counts how many even and odd numbers are present in the array.
Code:
public class EvenOddCount {
public static void main(String[] args) {
int[] arr = {10, 15, 20, 25, 30};
int evenCount = 0, oddCount = 0;
for (int num : arr) {
if (num % 2 == 0) evenCount++;
else oddCount++;
}
[Link]("Even numbers: " + evenCount);
[Link]("Odd numbers: " + oddCount);
}
}
5. Reverse an array
Explanation: Prints the array in reverse order using a loop.
Code:
public class ReverseArray {
public static void main(String[] args) {
int[] arr = {10, 20, 30, 40, 50};
[Link]("Original array:");
for (int num : arr) [Link](num + " ");
[Link]("\nReversed array:");
for (int i = [Link] - 1; i >= 0; i--) {
[Link](arr[i] + " ");
}
}
}
6. Copy elements from one array to another
Explanation: Copies elements from one array to another using a loop.
Code:
public class CopyArray {
public static void main(String[] args) {
int[] arr1 = {10, 20, 30, 40, 50};
int[] arr2 = new int[[Link]];
for (int i = 0; i < [Link]; i++) {
arr2[i] = arr1[i];
}
[Link]("Copied array:");
for (int num : arr2) [Link](num + " ");
}
}
7. Linear Search in an array
Explanation: Performs linear search to find an element in the array.
Code:
import [Link].*;
public class LinearSearch {
public static void main(String[] args) {
int[] arr = {10, 20, 30, 40, 50};
Scanner sc = new Scanner([Link]);
[Link]("Enter element to search: ");
int key = [Link]();
boolean found = false;
for (int i = 0; i < [Link]; i++) {
if (arr[i] == key) {
[Link]("Element found at position: " + (i + 1))
;
found = true;
break;
}
}
if (!found) [Link]("Element not found.");
}
}
8. Binary Search in a sorted array
Explanation: Implements binary search on a sorted array.
Code:
import [Link].*;
public class BinarySearch {
public static void main(String[] args) {
int[] arr = {10, 20, 30, 40, 50};
Scanner sc = new Scanner([Link]);
[Link]("Enter element to search: ");
int key = [Link]();
int low = 0, high = [Link] - 1;
boolean found = false;
while (low <= high) {
int mid = (low + high) / 2;
if (arr[mid] == key) {
[Link]("Element found at position: " + (mid + 1
));
found = true;
break;
} else if (arr[mid] < key) {
low = mid + 1;
} else {
high = mid - 1;
}
}
if (!found) [Link]("Element not found.");
}
}
9. Bubble Sort in ascending order
Explanation: Sorts the array in ascending order using bubble sort.
Code:
public class BubbleSortAsc {
public static void main(String[] args) {
int[] arr = {50, 30, 20, 40, 10};
for (int i = 0; i < [Link] - 1; i++) {
for (int j = 0; j < [Link] - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
[Link]("Sorted array:");
for (int num : arr) [Link](num + " ");
}
}
10. Bubble Sort in descending order
Explanation: Sorts the array in descending order using bubble sort.
Code:
public class BubbleSortDesc {
public static void main(String[] args) {
int[] arr = {10, 20, 30, 40, 50};
for (int i = 0; i < [Link] - 1; i++) {
for (int j = 0; j < [Link] - i - 1; j++) {
if (arr[j] < arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
[Link]("Sorted array in descending order:");
for (int num : arr) [Link](num + " ");
}
}
11. Selection Sort in ascending order
Explanation: Sorts the array using selection sort.
Code:
public class SelectionSort {
public static void main(String[] args) {
int[] arr = {64, 25, 12, 22, 11};
for (int i = 0; i < [Link] - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < [Link]; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
int temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
}
[Link]("Sorted array:");
for (int num : arr) [Link](num + " ");
}
}
12. Insertion Sort in ascending order
Explanation: Sorts the array using insertion sort.
Code:
public class InsertionSort {
public static void main(String[] args) {
int[] arr = {12, 11, 13, 5, 6};
for (int i = 1; i < [Link]; 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;
}
[Link]("Sorted array:");
for (int num : arr) [Link](num + " ");
}
}
13. Find the second largest element in an array
Explanation: Finds the second largest element by tracking two variables.
Code:
public class SecondLargest {
public static void main(String[] args) {
int[] arr = {10, 20, 4, 45, 99};
int largest = Integer.MIN_VALUE;
int secondLargest = Integer.MIN_VALUE;
for (int num : arr) {
if (num > largest) {
secondLargest = largest;
largest = num;
} else if (num > secondLargest && num != largest) {
secondLargest = num;
}
}
[Link]("Second largest: " + secondLargest);
}
}
14. Find the frequency of each element in an array
Explanation: Uses a HashMap to count frequency of each element.
Code:
import [Link].*;
public class FrequencyCount {
public static void main(String[] args) {
int[] arr = {10, 20, 10, 30, 20, 10};
Map<Integer, Integer> freqMap = new HashMap<>();
for (int num : arr) {
[Link](num, [Link](num, 0) + 1);
}
[Link]("Frequency of elements:");
for ([Link]<Integer, Integer> entry : [Link]()) {
[Link]([Link]() + ": " + [Link]());
}
}
}
15. Merge two arrays into one
Explanation: Combines two arrays into a single array.
Code:
public class MergeArrays {
public static void main(String[] args) {
int[] arr1 = {1, 2, 3};
int[] arr2 = {4, 5, 6};
int[] merged = new int[[Link] + [Link]];
int index = 0;
for (int num : arr1) merged[index++] = num;
for (int num : arr2) merged[index++] = num;
[Link]("Merged array:");
for (int num : merged) [Link](num + " ");
}
}
16. Sort names in alphabetical order
Explanation: Sorts an array of strings alphabetically using [Link].
Code:
import [Link].*;
public class SortNames {
public static void main(String[] args) {
String[] names = {"John", "Alice", "Bob", "Daisy"};
[Link](names);
[Link]("Sorted names:");
for (String name : names) [Link](name);
}
}
17. Check if an array is sorted or not
Explanation: Checks if the array is sorted in ascending order.
Code:
public class CheckSorted {
public static void main(String[] args) {
int[] arr = {10, 20, 30, 40, 50};
boolean sorted = true;
for (int i = 0; i < [Link] - 1; i++) {
if (arr[i] > arr[i + 1]) {
sorted = false;
break;
}
}
if (sorted) [Link]("Array is sorted.");
else [Link]("Array is not sorted.");
}
}
18. Find duplicate elements in an array
Explanation: Uses sets to find duplicate elements in the array.
Code:
import [Link].*;
public class FindDuplicates {
public static void main(String[] args) {
int[] arr = {10, 20, 10, 30, 20, 40};
Set<Integer> seen = new HashSet<>();
Set<Integer> duplicates = new HashSet<>();
for (int num : arr) {
if (![Link](num)) {
[Link](num);
}
}
[Link]("Duplicate elements: " + duplicates);
}
}
19. Delete an element from an array
Explanation: Deletes an element by shifting elements to the left.
Code:
public class DeleteElement {
public static void main(String[] args) {
int[] arr = {10, 20, 30, 40, 50};
int deleteIndex = 2; // delete element at index 2
for (int i = deleteIndex; i < [Link] - 1; i++) {
arr[i] = arr[i + 1];
}
[Link]("Array after deletion:");
for (int i = 0; i < [Link] - 1; i++) {
[Link](arr[i] + " ");
}
}
}
20. Insert an element at a given position in an array
Explanation: Inserts a new element by shifting elements to the right.
Code:
public class InsertElement {
public static void main(String[] args) {
int[] arr = new int[6];
arr[0] = 10; arr[1] = 20; arr[2] = 30; arr[3] = 40; arr[4] = 50;
int insertPos = 2; // position to insert
int newElement = 25;
for (int i = [Link] - 1; i > insertPos; i--) {
arr[i] = arr[i - 1];
}
arr[insertPos] = newElement;
[Link]("Array after insertion:");
for (int num : arr) [Link](num + " ");
}
}

You might also like