Array Operation
class ArrayOperations {
int[] array;
static int size;
void swap(int a, int b) {
array[a] = array[a] + array[b];
array[b] = array[a] - array[b];
array[a] = array[a] - array[b];
}
void displayArray() {
Arrays.stream(array).forEach(num -> System.out.print(num + " "));
System.out.println();
}
void displaySortedArray() {
for(int i = 0; i <= size; i++) {
System.out.print(array[i] + " ");
}
System.out.println();
}
int get(int index) {
if(index >= 0 && index < array.length) {
return array[index];
}
return -1;
}
void set(int index, int data) {
if(index >= 0 && index < array.length) {
array[index] = data;
}
}
int max() {
int max = array[0];
for(int i = 1; i < array.length; i++) {
if(array[i] > max) {
max = array[i];
}
}
return max;
}
int min() {
int min = array[0];
for(int i = 1; i < array.length; i++) {
if(array[i] < min) {
min = array[i];
}
}
return min;
}
int sum() {
int sum = 0;
for(int i = 0; i < array.length; i++) {
sum += array[i];
}
return sum;
}
float avg() {
return (float) sum() / array.length;
}
void reverse() {
int i = 0, j = array.length - 1;
while(i < j) {
swap(i++, j--);
}
}
boolean isArraySorted() {
for(int i = 1; i <= size; i++) {
if(array[i-1] > array[i]) {
return false;
}
}
return true;
}
boolean insertInSortedArray(int x) {
// Do nothing if array is full
if(size == array.length - 1) {
return false; // array is full
}
// Insert at 0th index if array is empty
if(size == -1) {
array[++size] = x;
return true;
}
if(isArraySorted()) {
int i = size;
while (i >= 0 && array[i] > x) { // right shift elements to make space
array[i+1] = array[i];
i--;
}
array[i+1] = x; // insert successful
size++; //Since added new element so increase index
return true;
}
return false;
}
void insertInSortedArray() {
insertInSortedArray(15);
insertInSortedArray(12);
insertInSortedArray(13);
insertInSortedArray(16);
insertInSortedArray(11);
insertInSortedArray(14);
insertInSortedArray(18);
insertInSortedArray(19);
displaySortedArray();
}
void arrangeAllNegativeInLeftAndPositiveInRight() {
array = new int[] {-3, 5, -6, 7, -8, 9, -64, 42, -3, 65};
displayArray();
int i = 0, j = array.length - 1;
// Repeat process until i < j
while(i < j) {
while(array[i] < 0) {
i++; // if -Ve number than move next
}
while(array[j] > 0) {
j--; // if +Ve number than move next
}
// Here we found +ve number in left side and -ve number in right side
if(i < j) {
swap(i, j); // swap elements at ith and jth index
}
}
displayArray();
}
public static void main (String[] args) {
ArrayOperations obj = new ArrayOperations();
obj.array = new int[] {3, 5, 6, 7, 8, 9, 64, 42, 3, 65};
System.out.println();
obj.displayArray();
obj.swap(3,4);
obj.displayArray();
System.out.println(obj.get(5));
obj.set(1, 100);
obj.displayArray();
System.out.println("\nmax : " + obj.max());
System.out.println("min : " + obj.min());
System.out.println("sum : " + obj.sum());
System.out.println("avg : " + obj.avg());
obj.displayArray();
obj.reverse();
obj.displayArray();
System.out.println("Is array sorted? : " + obj.isArraySorted());
obj.array = new int[] {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
size = -1;
obj.insertInSortedArray();
obj.arrangeAllNegativeInLeftAndPositiveInRight();
}
}
Comments
Post a Comment