Google Pen Test--sort, allow only 0 and other elements to exchange

Source: Internet
Author: User

2.2 Arrays of length n are stored in random order from 0 to n-1. Now you can do only 0 swap with other numbers, design and implement sorting.

The problem has an implied condition: that the array element is contiguous, that is, 0--n-1, when you have sequenced, you will find that the array element and the element's subscript are equal.

Idea: Take array 2 0 3 1 For example

1, first a[0]=2, according to the above conditions it should be placed in the position of a[2]. Because only 0 elements and other elements are allowed to be exchanged. So we can not directly exchange a[0] and a[2], so the 0 elements and A[2] interchange, get 2 3 0 1, then the a[0]=2 and a[2]=0 will be interchangeable, get 0 3 2 1

Following this idea, the implementation code is as follows:

//sort_google.cpp: Defines the entry point of the console application. //#include"stdafx.h"#include<iostream>using namespacestd;voidSwapint*a,intIintj) {    inttemp; Temp=A[i]; A[i]=A[j]; A[J]=temp;}intFindint*a,intLen) {     for(intI=0; i<len;i++)    {        if(a[i]==0)        {            returni; }    }}voidSortint*a,intLen) {     for(intI=0;i<len;)//notice there's no i++ here .    {        if(a[i]==i) {i++;//to be able to jump out of the loop is the main thing to see            Continue;        } Swap (A,find (A,len), a[i]);  for(intj=0; j<len;j++) {cout<<a[j]<<"  ";//This is just for the output array element to look at the element value of the array after sorting has no practical effect} cout<<Endl;        Swap (A,find (A,len), i);  for(intj=0; j<len;j++) {cout<<a[j]<<"  ";//This is just for the output array element to look at the element value of the array after sorting has no practical effect} cout<<Endl; Swap (A,find (A,len),0);//Place the 0 element in the a[0] position.     }    }int_tmain (intARGC, _tchar*argv[]) {    inta[]={1,Ten,9,7,5,2,3,4,0,6,8};//{3,2,0,1};    intlen=sizeof(a)/sizeof(int);    Sort (A,len);  for(intI=0; i<len;i++) {cout<<a[i]<<"  "; } cout<<Endl; return 0;}

As follows:

Google Pen Test--sort, allow only 0 and other elements to exchange

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.