GithubHelp home page GithubHelp logo

algs4's Introduction

Code about book "Algorithms 4th Edition".

Sort

Insert sort

	public void doSort(Comparable[] items) {
		for(int i = 1; i<items.length; i++){
			for(int j = i;j > 0 && less(items[j],items[j-1]); j--){
				exchange(items, j, j - 1); 
			}
		}
	}

Alt Insert sort

Select sort

	public void doSort(Comparable[] items) {
		for(int i = 0; i<items.length; i++){
			int minIndex = i;
			for(int j = i + 1;j<items.length; j++){
				if(less(items[j],items[minIndex])){
					minIndex = j;
				}
			}
			exchange(items, i, minIndex);
		}
	}

Alt Select sort

Shell sort

	public void doSort(Comparable[] items) {
		int N = items.length;
		int h = 1;
		while(h<N) h = h*3 + 1;
	    
		while(h>0){
			for(int i = 0;i<N;i++){
				List<Integer> highlight = new ArrayList<Integer>();
				for(int j = i;j>=h && less(items[j],items[j - h]); j-=h){
					exchange(items,j,j - h);
				}
			}
			h/=3;
		}
	}

Alt Shell sort

Merge sort

	public void doSort(Comparable[] items) {
		aux = new Comparable[items.length];
		sort(items,0,items.length  - 1);
	}
	
	private void sort(Comparable[] items,int lo,int hi){
		if(hi<=lo) return;
		int mid = lo + (hi - lo) / 2;
		sort(items,lo,mid);
		sort(items,mid + 1,hi);
		merge(items,lo,mid,hi);
	}

	private void merge(Comparable[] items,int lo,int mid,int hi){
		int i = lo,j = mid + 1;
		for(int k = lo; k <= hi; k++){
			aux[k] = items[k];
		}
		for(int k = lo; k <= hi; k++){
			if(i > mid){
				items[k] = aux[j];
				j++;
			}else if(j > hi){
				items[k] = aux[i];
				i++;
			}else if(less(aux[i] , aux[j])){
				items[k] = aux[i];
				i++;
			}else{
				items[k] = aux[j];
				j++;
			}
		}
	}

Alt Merge sort

Quick sort

	public void doSort(Comparable[] items) {
		sort(items,0,items.length - 1);
	}
	
	private void sort(Comparable[] items,int lo,int hi){
		if(hi<=lo) return;
		int j = paratition(items,lo,hi);
		sort(items,lo,j);
		sort(items,j+1,hi);
	}

	private int paratition(Comparable[] items, int lo, int hi) {
		
		//lo mark
		int i = lo;
		//hi mark
		int j = hi + 1;
		Comparable v = items[lo];
		
		while(true){
			
			//find an item bigger than v
			while(less(items[++i],v)) if(i==hi) break;
			//find an item smaller than v
			while(less(v,items[--j])) if(j==lo) break;
			
			// when lo mark meet hi mark then stop all(sort complate)
			if(i>=j) break;
			
			//exchange smaller item and bigger item from left to right
			exchange(items, i, j);
			
		}
		exchange(items, lo, j);
		return j;
	}
	

Alt Quick sort

Note

Video record by Bandicam. Use 格式工厂 to convert MP4 file to GIF picture.

Code in github:algs4.

algs4's People

Contributors

at0x7c00 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.