Fast sorting algorithm C,java code __c language

Source: Internet
Author: User
Re-learn some basic algorithms, write some basic.

Here the order of the original array of algorithms are static random knock a few values in, can be modified to dynamically generate array elements (changed to post it)

C Language code:

#include <stdio.h> void Print_arr (int i_arr[], int arr_size) {for (int i = 0; i < arr_size; i++) {printf ("%d
	\ t ", * (I_arr+i));
printf ("\ n");
	} void Swap (int *a, int *b) {int tmp = *A;
	*a = *b;
*B = tmp; /** * Partition the array, * @i_arr: Array * @param p:first element Index * @parma r:last element index/int parti
	tion (int i_arr[], int p, int r, int arr_size) {int pos = p-1;
	int base = I_arr[r];
			for (int i = p; i < R; i++) {if (I_arr[i] <= base) {pos++;
		Swap (&i_arr[i], &i_arr[pos]);
	} else {}//base = I_arr[i];
	} swap (&i_arr[++pos], &i_arr[r]);
	Print_arr (I_arr, arr_size);
return POS; /** * Main algorithm/void quick_sort (int i_arr[], int p, int r, int arr_size) {if (P < r) {int pos = Partitio
		N (I_arr, p, R, Arr_size);
		printf ("pos:%d\n", POS);
		Quick_sort (I_arr, p, pos-1, arr_size);
	Quick_sort (I_arr, pos + 1, R, Arr_size); int main () {int i_arr[] = {10, 23, 5, 8, 12, 15, 20, 8, 16, 30, 85, 20};
Quick_sort (I_arr, 0, sizeof (i_arr)/sizeof (int)-1, sizeof (I_arr)/sizeof (int)); }

Java code:

Import static java.lang.System.out;

public class quick_sort{public
	static void Main (string[] args) {
		int i_arr[] = {10, 6, 32, 2, 390, 34, 1, 48, 3, 1 8, 8};
		Quick_sort (I_arr, 0, i_arr.length-1);
		Print_arr (I_arr);
	}
	
	/**
	* * * *
	private static void Quick_sort (int arr[], int start, int end) {
		if (Start < end) {
			int P OS = partition (arr, start, end);
			Quick_sort (arr, start, pos-1);
			Quick_sort (arr, pos + 1, end);
		}
	
	/**
	* * @return
	POS
	/private static int partition (int arr[], int start, int end) {
		int pos = start-1 ;
		int base = Arr[end];
		
		for (int i = start, I < end; i++) {
			if (Arr[i] < base) {
				pos++;
				int tmp = Arr[pos];
				Arr[pos] = Arr[i];
				Arr[i] = tmp;
			}
		}
		
		Arr[end] = Arr[++pos];
		Arr[pos] =  base;
		return pos;
	}
	
	/**
	* * * *
	private static void Print_arr (int[] arr) {for
		(int e:arr) {
			out.print (e + "T"); 
  }
		Out.println ("");
	}
}


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.