Fast sorting algorithm C and Java code

Source: Internet
Author: User

Re-learn some basic algorithms and write some basic ones.

The original array of the Sorting Algorithm here is static and randomly typed into a few values. You can modify it to dynamically generate array elements (modified and pasted)

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 partition(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 = partition(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, 18, 32, 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 pos = 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("");}}

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.