Sorts unordered array elements and outputs the position of the original element corresponding to the sorted element after sorting

Source: Internet
Author: User
Tags sorts

Topic:

Sorts unordered array elements and outputs the position of the original element corresponding to the sorted element after sorting

(1) Method 1

Method 1: First record the original corresponding position of the array element in another array
And in the process of selecting sorting, exchanging array elements also exchange corresponding element values in the corresponding position array

/*The selection method sorts and sorts the array elements in the corresponding position of the original array to output Method 1: First record the corresponding position of the array element in the other array and in the process of selecting sorting, exchanging the array element while exchanging the corresponding element value in the corresponding position array*/#include<iostream>using namespacestd;intMain () {inta[ One],i,j,k,t;//a array record element values intf[ One];//the original array element position in the F array record acout<<"Enter the ORIGINL array:"<<Endl; for(i=1; i<=Ten; i++) cin>>a[i];//Enter 10 numbers  for(i=1; i<=Ten; i++) f[i]=i;//Initialize the position of the original array element//sorting by Selection method  for(i=1; i<=9; i++) {k=i;  for(j=i+1; j<=Ten; j + +)                           if(A[j]<a[k]) k=J; //Exchange A[k] and A[i]t=A[k]; A[K]=A[i]; A[i]=T; //Exchange F[k] and F[i], the record position will change with the data exchanget=F[k]; F[K]=F[i]; F[i]=T; } cout<<"The sorted array:"<<Endl; for(i=1; i<=Ten; i++) cout<<a[i]<<" ";//outputs 10 ordered numbers in a rowcout<<Endl; cout<<"Putout the location of originl array:"<<Endl; for(i=1; i<=Ten; i++) cout<<f[i]<<" "; cout<<Endl;return 0;}

(2) Method 2

Method 2: First store the array elements in another array
Sort
Finds the corresponding position in the original array for the sorted array element in turn

/*The selection method sorts and sorts the array elements in the corresponding position of the original array to output Method 2: The array element is first placed in another array to sort the sorted array elements in turn to find the corresponding position in the original array*/#include<iostream>using namespacestd;#defineN 10intMain () {inta[n+1],i,j,k,t,flag;//a array records element values, and flag indicates where to find intb[n+1];//the B array holds all the element values in the a arraycout<<"Enter the ORIGINL array:"<<Endl; for(i=1; i<=n;i++) cin>>a[i];//Enter 10 numbers  for(i=1; i<=n;i++) B[i]=a[i];//Initialize the position of the original array element//sorting by Selection method  for(i=1; i<=n-1; i++) {k=i;  for(j=i+1; j<=n;j++)                           if(A[j]<a[k]) k=J; //Exchange A[k] and A[i]t=A[k]; A[K]=A[i]; A[i]=T; } cout<<"The sorted array:"<<Endl; for(i=1; i<=n;i++) cout<<a[i]<<" ";//outputs 10 ordered numbers in a rowcout<<Endl; cout<<"Putout the location of originl array:"<<Endl; for(j=1; j<=n;j++)    {  if(j==1)//find the position of the first element after sorting{ for(i=1; i<=n;i++)       if(a[j]==B[i]) {cout<<i<<" "; Flag=i;//record the location that is currently found         Break; }    }    Else   //search for elements other than an element after sorting{if(a[j-1]==A[J])//if the sorted adjacent two elements are equal, that is, the current element is the same as the element that was previously found{ for(i=flag+1; i<=n;i++)//Search from the location where you last found the flag          if(a[j]==B[i]) {cout<<i<<" "; Flag=i;//record the location that is currently found            Break; }        }      Else   //the current element is not the same as the element that was previously found{ for(i=1; i<=n;i++)        if(a[j]==B[i]) {cout<<i<<" "; Flag=i;//record the location that is currently found          Break; } }}} cout<<Endl;return 0;}

Sorts unordered array elements and outputs the position of the original element corresponding to the sorted element after 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.