[Go to site: main page, start]

0% found this document useful (0 votes)
5 views3 pages

Java Array Management and Sorting

The document contains a Java program that defines a main class with nested classes for managing students and an array. The Array_manager class includes methods for adding items, sorting the array using insertion sort, and performing a binary search. The main method demonstrates the functionality by adding students, sorting their IDs, and searching for a specific ID.

Uploaded by

firedrop421
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)
5 views3 pages

Java Array Management and Sorting

The document contains a Java program that defines a main class with nested classes for managing students and an array. The Array_manager class includes methods for adding items, sorting the array using insertion sort, and performing a binary search. The main method demonstrates the functionality by adding students, sorting their IDs, and searching for a specific ID.

Uploaded by

firedrop421
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

import [Link].

ArrayList;

public class main {

public class Student{


public String name;
public int studentID;

public Student(String name, int studentID){


[Link] = name;
[Link] = studentID;
}

public class Array_manager{


private int[] list;
private int lenght;
private int numitems;

public Array_manager(int size) {


numitems = 0;
lenght = size;
list = new int[size];
}

public boolean add(int item) {


if (numitems < lenght) {
list[numitems] = item;
numitems++;
return true;

}
return false;
}
public void insertionSort() {
for (int start = 1; start < numitems; start++) {
int temp = list[start];
int prev = start - 1;
while (prev >= 0 && list[prev] > temp) {
list[prev + 1] = list[prev];
prev--;
}
list[prev + 1] = temp;
}
}

public int binarysearch(int key){


int low = 0;
int high = numitems - 1;
while (low <= high) {
int mid = (low + high) / 2;
if (key == list[mid]) return mid;
if (key < list[mid]) high = mid - 1;
else low = mid + 1;
}
return -1;
}
public void printArray() {
[Link]("Array: ");
for (int i = 0; i < numitems; i++) {
[Link](list[i] + " ");
}
[Link]();
}

}
public static void main(String[] args) {
main N = new main();
main.Array_manager ml = [Link] Array_manager(5);
ArrayList<[Link]> students = new ArrayList<>();

[Link]([Link] Student("Julio", 50)); [Link](50);


[Link]([Link] Student("Julia", 30)); ...
[Link]([Link] Student("Neto", 40)); ...
[Link]([Link] Student("Crist", 10)); ...
[Link]([Link] Student("Pedro", 20)); ...

[Link]();

[Link]("\nAfter Sorting:");
[Link]();

int searchKey = 40;


int foundIndex = [Link](searchKey);
[Link]("\nBinary Search for "+ + searchKey + ": " + foundIndex);
}
}

You might also like