C [Code instance] integer array binary sorting

Source: Internet
Author: User

#include <stdio.h> #include <stdlib.h> #include <stdbool.h> #include <string.h>void _qsort ( void*, size_t, size_t); void Vswap (void*, void*, size_t); int main (void) {int int_1[] = {85,9,32,64,12,7,9,51,2,63};size_t len = sizeof (int);/*char int_1[] = {85,9,32,64,12,7,9,51,2,63};size_t len = sizeof (char); */size_t count = sizeof (int_1)/L En;void *p_1 = malloc (Count*len), memcpy (P_1, Int_1, Count*len), _qsort (P_1, Count, Len);p rintf ("All Done-----------------\ n "); size_t Total_len = len * Count;int i = 0;for (; i < Total_len; ++i) {if (I%len! = 0) {continue;} printf ("%d,", * (int *) (p_1+i))),//printf ("%d\n", * ((char *) (p_1+i))); printf ("\ n");} void _qsort (void *p, size_t count, size_t len) {if (count = = 2) {if (* (int*) (P)) > * ((int*) (P+len)) {Vswap (P, P+len, L EN);}}  if (Count > 2) {int Mid_edge = (count + count%2)/2-1;size_t Total_len = Len * count;void *p_t = malloc (total_len); int i = 0;int Index = 0;int L_count = 0;int R_count = 0;for (; i < Total_len; ++i) {if (I%len!)= 0) {continue;} if (I/len = = Mid_edge) {continue;} if (* (int*) (p+i)) <= * ((int*) (p+ (Mid_edge*len))) {memcpy (p_t+ (Index*len), P+i, Len);p rintf ("L:%d, Val:%d\n", Index, * ((int*) (p+i)); index++;l_count++;}} Set midmemcpy (p_t+ (Index*len), p+ (Mid_edge*len), Len);p rintf ("Mid_edge:%d, Val:%d\n", Index, * (int*) (p+ (mid_edge* Len))); Index++;//set Righti = 0;for (; i < Total_len; ++i) {if (I%len! = 0) {continue;} if (I/len = = Mid_edge) {continue;} if (* (int*) (p+i) > * ((int*) (p+ (Mid_edge*len))) {memcpy (p_t+ (Index*len), P+i, Len);p rintf ("R:%d, Val:%d\n", index , * ((int*) (p+i)); index++;r_count++;}} memcpy (P, p_t, Total_len), free (p_t),//recursiveprintf ("%d\n", L_count);p rintf ("%d\n", R_count);p rintf (" Done-----------------\ n "); _qsort (P, L_count, Len), _qsort (p+ ((l_count+1) *len), R_count, Len);}} void Vswap (void *p_1, void *p_2, size_t len) {void *p_t = malloc (len), memcpy (p_t, P_1, Len), memcpy (P_1, p_2, Len), memcpy (p_ 2, p_t, Len); free (p_t);}

C [Code instance] integer array binary sorting

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.