Java implementation of bubble sort, quick sort (quick sort), and KMP algorithm

People are too lazy to post an article for a long time. Write your own algorithm implementation today. It's relatively simple and may be laughable, but I think it's better to record it.


  Two days ago, someone in the QQ group said that his boss didn't understand Java but was recruiting Java engineers. Therefore, we choose the greatest common divisor that has nothing to do with language and can examine the ability--algorithm. Probably bubble sort, quick sort (quick sort), binary search, KMP algorithm.
  Everyone knows how to do Java, and it can be easily sorted by means of Comparable and Comparator, so everyone is usually rusty with these basic algorithms. In order to exercise my own algorithm logic, I tried to implement it myself. It may be very similar to the algorithm implementation you are looking for. It can only be said that the implementation of simple algorithms is still relatively difficult to innovate.
Tested by Junit, some tools and methods of apache common are introduced, but these are not important, let's look at the code.

package algorithm;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;
import java.util.stream.Stream;

import org.junit.Test;

public class SortTest {

	private Integer[] generateTestArray() {

		ArrayList<Integer> originList = new ArrayList<>();
		Random random = new Random(LocalDateTime.now().toEpochSecond(ZoneOffset.ofHours(8)));
		Stream.generate(random::nextInt).limit(20).forEach(originList::add);

		System.out.println(originList);
		Integer[] tempArr = new Integer[20];
		originList.toArray(tempArr);
		return tempArr;
	}

	@Test
	public void nnnnn() {

		Integer[] tempArr = generateTestArray();
		for (int i = 0; i < tempArr.length - 1; i++) {

			boolean swapFlag = false;
			for (int j = 0; j < tempArr.length - 1 - i; j++) {

				if (tempArr[j] > tempArr[j + 1]) {
					int temp = tempArr[j];
					tempArr[j] = tempArr[j + 1];
					tempArr[j + 1] = temp;
					swapFlag = true;
				}
			}
			if (swapFlag == false) {
				break;
			}
		}
		Arrays.stream(tempArr).forEach(System.out::println);
	}

	public int middleIndex(Integer[] tempArray, int low, int high) {

		int temp = tempArray[low];
		while (low < high) {

			while (low < high && tempArray[high] > temp) {
				high--;
			}
			tempArray[low] = tempArray[high];
			while (low < high && tempArray[low] < temp) {
				low++;
			}
			tempArray[high] = tempArray[low];
		}
		tempArray[high] = temp;
		return high;
	}

	public void recursiveByQuickSort(Integer[] tempArray, int low, int high) {

		if (low < high) {

			int middleIndex = middleIndex(tempArray, low, high);
			recursiveByQuickSort(tempArray, low, middleIndex - 1);
			recursiveByQuickSort(tempArray, middleIndex + 1, high);
		}
	}

	@Test
	public void quickSort() {

		Integer[] tempArray = generateTestArray();
		recursiveByQuickSort(tempArray, 0, tempArray.length - 1);
		System.out.println(Arrays.asList(tempArray));

		tempArray = new Integer[]{-2127313485, -2022230754, -1525774655, -1266487529, -1246088751, -1237557299, -1220678150, -669061349, -587792801, -441728115, -388929620, -228781073, -5476750, 424415588, 631901841, 878929975, 1519073389, 1526646025, 1791609759, 2002233420};
		System.out.println(Arrays.asList(tempArray));
		System.out.println(binaryQuery(tempArray, -587792801));
	}

	private int binaryQuery(Integer[] tempArray, int val) {

		int low = 0, high = tempArray.length - 1;
		while (low < high) {

			int middle = (low + high) / 2;
			if (tempArray[middle] < val) {
				low = middle + 1;
			} else if (tempArray[middle] > val) {
				high = middle - 1;
			} else {
				return middle;
			}
		}
		if (low == high && tempArray[low] == val) {
			return low;
		}
		return -1;
	}
}



package algorithm;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;

public class KMPTest {
	
	private int[] suffixMatchPrefixMatchSequence(String queryStr) {
		
		char[] charArray = queryStr.toCharArray();
		int charLength = charArray.length,
				lastIndex = charLength - 1;
		
		if (charLength <= 2) {
			
			return null;
		}
		
		final int[] matchSequence = new int[charLength];
		outFor : for (int i = 1; i < charLength; i++) {
			int tempI = i;
			for (int j = 0; j <lastIndex && tempI < charLength; j++, tempI++) {
				char ic = charArray [tempI];
				char jc = charArray [j];
				if (ic != jc) {
					matchSequence [tempI] = 0;
					break;
				}
				matchSequence[tempI] = j + 1;// Do offset subtraction, so start from 1
				if (tempI == lastIndex) {
					break outFor;
				}
			}
		}
		System.out.println(StringUtils.join(matchSequence, ','));
		return matchSequence;
	}
	
	public int matchedStartIndex(String targetStr, String queryStr) {
		
		char[] targetCharArray = targetStr.toCharArray(),
				queryCharArray = queryStr.toCharArray();
		
		int targetCharArrayLength = targetCharArray.length,
				queryCharArrayLength = queryCharArray.length,
				matchStartIndex = 0,
				queryLastIndex = queryCharArrayLength - 1;
		final int[] matchSequence = this.suffixMatchPrefixMatchSequence(queryStr);
		while (matchStartIndex < targetCharArrayLength) {
			
			int tempMatchStartIndex = matchStartIndex;
			int i = 0;
			for (; i < queryCharArrayLength && tempMatchStartIndex < targetCharArrayLength; i++, tempMatchStartIndex++) {
				
				char qc = queryCharArray[i],
						tc = targetCharArray[tempMatchStartIndex];
				if (qc == tc) {
					continue;
				}
				break;
			}
			if (i > queryLastIndex) {// The +1 operation will be performed after the exact match, so the condition is added >
				
				return matchStartIndex;
			}
			
			matchStartIndex += ArrayUtils.isEmpty(matchSequence) ? 1: (i - matchSequence[i]) == 0? 1: i - matchSequence[i];
		}
		
		return -1;
	}
	
	
	@Test
	public void test() {
		
		String targetStr = "56132asdasdasfsf4dasa123",
				queryStr = "dasa123";
		System.out.println(this.matchedStartIndex(targetStr, queryStr));
	}
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326173441&siteId=291194637