Created
May 16, 2023 07:01
-
-
Save G36maid/bba67c390a91732d005bcefed4765d6a to your computer and use it in GitHub Desktop.
Sort.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include<stdio.h> | |
| #include<stdint.h> | |
| #include<stdlib.h> | |
| void swap(int *p1,int *p2){ | |
| int temp; | |
| temp=*p1; | |
| *p1=*p2; | |
| *p2=temp; | |
| } | |
| void myquickSort(int *idx,int *value, int left, int right ){ | |
| if(left<right){ | |
| int s=value [idx[(left+right)/2]]; | |
| int i=left-1; | |
| int j=right+1; | |
| while(1){ | |
| while(value[idx[++i]]<s);//find right | |
| while(value[idx[--j]]>s);//find left | |
| if(i>=j){ | |
| break; | |
| } | |
| swap(&idx[i],&idx[j]); | |
| } | |
| myquickSort(idx,value, left , i-1);//recursive left part | |
| myquickSort(idx,value, j+1 ,right);//recursive right part | |
| } | |
| } | |
| void quick_sort(int *idx,int * value,int len){ | |
| //index[]; value[]; | |
| myquickSort(idx,value,0,len-1); | |
| } | |
| int main(){ | |
| int n; | |
| scanf("%d",&n); | |
| int value[n],idx[n]; | |
| for(int i=0;i<n;i++){ | |
| idx[i]=i; | |
| scanf("%d",&value[i]); | |
| } | |
| printf("idx:\n"); | |
| for(int i=0;i<n;i++){ | |
| printf("%d ",idx[i]); | |
| } | |
| printf("\nvalue:\n"); | |
| for(int i=0;i<n;i++){ | |
| printf("%d ",value[i]); | |
| } | |
| printf("\n"); | |
| quick_sort(idx,value,n); | |
| printf("idx:\n"); | |
| for(int i=0;i<n;i++){ | |
| printf("%d ",idx[i]); | |
| } | |
| printf("\nvalue:\n"); | |
| for(int i=0;i<n;i++){ | |
| printf("%d ",value[i]); | |
| } | |
| printf("\nsorted:\n"); | |
| for(int i=0;i<n;i++){ | |
| printf("%d ",value[idx[i]]); | |
| } | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment