Algorithm and data structure 2

Algorithms and data structures 02 linear and binary lookups (queues and stacks)

Linear search: sequential search, slow

```package com.kele;

import java.sql.SQLOutput;
import java.util.Arrays;

/**Queue: first in, first out
* @author 12402
*/
public class MyQueue {

private int[] arr;

public MyQueue() {
arr = new int[0];
}

// Add elements to the queue
public void add(int target){
int[] newArr = new int[arr.length + 1];
for (int i = 0; i < arr.length; i++) {
newArr[i] = arr[i];
}

newArr[newArr.length-1] = target;
arr = newArr;

}

// Show queue elements
public void show(){
System.out.println(Arrays.toString(arr));
}

// Take out the first element of Jingru
public int pop(){
int target = arr[arr.length - 1];
int[] newArr = new int[arr.length - 1];

for (int i = 0; i < newArr.length; i++) {
newArr[i] = arr[i];
}
newArr = arr;
return target;
}

// Take out the last element
public int pop2(){
int target = arr[0];
int[] newArr = new int[arr.length - 1];

for (int i = 0; i < newArr.length; i++) {
newArr[i] = arr[i];
}
newArr = arr;
return target;
}

public boolean isEmpty(){
return arr.length == 0;
}
}

```

Binary search: faster query speed

```package com.kele;

/** binary search
* @author 12402
*/
public class BinarySearch {

public static void main(String[] args) {

// Define target array
int[] arr = {1,2,3,4,5,6,7,8};

// Target element to find
int target = 5;

// Start of search
int begin = 0;

// End of lookup
int end = arr.length-1;

//Middle position
// int mid = (arr.length)/2;
int mid = (begin + end)/2;

//Element subscript
int index = -1;

// Loop lookup
while (true){
// The target element is equal to the middle element of the array
if (arr[mid] == target){
index = mid;
break;
}else {
// Target element is less than middle element
if (arr[mid] > target) {

end = mid - 1;

// Target element is greater than middle element
} else {
begin = mid + 1;
}

mid = (begin + end)/2;
}
}
System.out.println("Element subscript:" + index);
}
}

```

Query in object array (integration)

```package com.kele;

import java.util.Arrays;

public class MyArray {

private int[] arr;

public MyArray() {
arr = new int[0];
}

// Array length
public int  size(){
return arr.length;
}

public void show(){
System.out.println(Arrays.toString(arr));
}

//Add element at the end of array
public void add(int a){

int[] newArr = new int[arr.length + 1];
for (int i = 0; i < arr.length; i++) {
newArr[i] = arr[i];
}
newArr[arr.length] = a;
arr = newArr;
}

//Delete elements
public void delete(int index){
if (index < 0 || index > arr.length - 1){
throw new RuntimeException("Out of array range");
}
int[] newArr = new int[arr.length -1];
for (int i = 0; i < newArr.length; i++) {

if (i < index){
newArr[i] = arr[i];
}else {
newArr[i] = arr[i + 1];
}
}
arr = newArr;
}

// Insertion element
public void insert(int index,int b){
if (index < 0 || index > arr.length - 1){
throw new RuntimeException("Out of array range");
}

int[] newArr = new int[arr.length + 1];
for (int i = 0; i < arr.length; i++) {
if (i < index){
newArr[i] = arr[i];
}else {
newArr[i + 1] = arr[i];
}
}
newArr[index] = b;
arr = newArr;
}

// Get the specified element
public void get(int index){
if (index < 0 || index > arr.length - 1){
throw new RuntimeException("Out of array range");
}
int c = arr[index];
System.out.println(c);
}

// Replace specified element
public void set(int index,int d){
if (index < 0 || index > arr.length - 1){
throw new RuntimeException("Out of array range");
}
arr[index] = d;
}

// Linear search
public int  search(int target){
for (int i = 0; i < arr.length; i++) {
// Find the corresponding target element in the array
if (arr[i] == target) {
// Target element value subscript found
return i;
}
}
return -1;
}

public int binarySearch(int target){
// Start of search
int begin = 0;

// End of lookup
int end = arr.length-1;

//Middle position
// int mid = (arr.length)/2;
int mid = (begin + end)/2;

// Loop lookup
while (true){

// If no element is found, the start position is greater than the end position
if (begin >= end){
return -1;
}
// The target element is equal to the middle element of the array
if (arr[mid] == target){
return mid;
}else {
// Target element is less than middle element
if (arr[mid] > target) {

end = mid - 1;

// Target element is greater than middle element
} else {
begin = mid + 1;
}

mid = (begin + end)/2;
}
}
}
}

```

Stack: first in first out

```package com.kele;

import java.util.Arrays;

/** Stack: first in first out
* @author 12402
*/
public class MyStack {

private int[] arr;

public MyStack() {
arr = new int[0];
}

// Indentation element
public void push(int target){
int[] newArr = new int[arr.length + 1];
for (int i = 0; i < arr.length; i++) {
newArr[i] =  arr[i];
}
newArr[arr.length] = target;
arr = newArr;
}

public void show(){
System.out.println(Arrays.toString(arr));
}

// Take out the top element of the stack
public int pop(){
if (arr.length == 0){
throw new RuntimeException("There are no elements in the stack");
}

int top = arr[arr.length - 1];
int[] newArr = new int[arr.length -1];
for (int i = 0; i < newArr.length; i++) {
newArr[i] = arr[i];
}
arr = newArr;

}

// View top of stack elements
public int peek(){
// You can only get the stack top element, not the stack top element
return arr[arr.length -1 ];
}

public boolean isEmpty(){
return arr.length == 0;
}
}

```

queue

```package com.kele;

import java.sql.SQLOutput;
import java.util.Arrays;

/**Queue: first in, first out
* @author 12402
*/
public class MyQueue {

private int[] arr;

public MyQueue() {
arr = new int[0];
}

// Add elements to the queue
public void add(int target){
int[] newArr = new int[arr.length + 1];
for (int i = 0; i < arr.length; i++) {
newArr[i] = arr[i];
}

newArr[newArr.length-1] = target;
arr = newArr;

}

// Show queue elements
public void show(){
System.out.println(Arrays.toString(arr));
}

// Take out the first element of Jingru
public int pop(){
int target = arr[arr.length - 1];
int[] newArr = new int[arr.length - 1];

for (int i = 0; i < newArr.length; i++) {
newArr[i] = arr[i];
}
newArr = arr;
return target;
}

// Take out the last element
public int pop2(){
int target = arr[0];
int[] newArr = new int[arr.length - 1];

for (int i = 0; i < newArr.length; i++) {
newArr[i] = arr[i];
}
newArr = arr;
return target;
}

public boolean isEmpty(){
return arr.length == 0;
}
}

```

thought

• Adding an element is to add a new array, which is longer than the old array, and then assign the value of the old array to the new array,
At last, the new array value is assigned to the old array to complete the old array element
The increase of element.
```// Add elements to the queue
public void add(int target){
int[] newArr = new int[arr.length + 1];
for (int i = 0; i < arr.length; i++) {
newArr[i] = arr[i];
}

newArr[newArr.length-1] = target;
arr = newArr;

}
```
• To delete an element is to add a new array, which is shorter than the old array, and then assign the value of the old array to the new array,
One more element of the old array will be deleted. Finally, assign the value of the new array to the old array to complete the old array element
Delete element.
```//Delete elements
public void delete(int index){
if (index < 0 || index > arr.length - 1){
throw new RuntimeException("Out of array range");
}
int[] newArr = new int[arr.length -1];
for (int i = 0; i < newArr.length; i++) {

if (i < index){
newArr[i] = arr[i];
}else {
newArr[i] = arr[i + 1];
}
}
arr = newArr;
}
```
• Modify the array element, assign the modified value to the array subscript of the value to be modified, and complete the modification
``` // Replace specified element
public void set(int index,int d){
if (index < 0 || index > arr.length - 1){
throw new RuntimeException("Out of array range");
}
arr[index] = d;
}
```
Published 2 original articles, won praise 0 and visited 2

Tags: Java SQL less

Posted on Mon, 03 Feb 2020 12:47:43 -0500 by Canadian