Beispiel #1
0
int QuickSort::sort1(Array &array, int left, int right) const
{
	if(left >= right)
	{
		// do nothing if less than 1 elements
		return 0;
	}

	
	/// @note Nico Lomuto method
	int m = left;
	//	printf("sort(%d-%d)\n", left, right);	
	for(int i= left + 1; i <= right; i++)
	{
		if(array[i] < array[left])
		{
			array.Swap(++m, i);
		}
	}

	// @note without this swap, infinite-loop may occurs
	// if m is maximum number... 
	array.Swap(left, m); 
	
	sort1(array, left, m - 1);
	sort1(array, m + 1,right);
	return 0;
}
Beispiel #2
0
/* Merge sort $list and return its head and tail. */
static struct ero_st *sort1(struct ero_st *list, unsigned nlist,
      struct ero_st **rightendp)
{
   unsigned nleft;
   struct ero_st *left, *leftend;
   struct ero_st *right, *rightend;

   if (nlist <= 1)
   {
      *rightendp = list;
      return list;
   } else if (nlist == 2)
   {  /* Make sure (*rightendp)->next == start of next list. */
      if (compare(list, list->next) <= 0)
      {
         *rightendp = list->next;
         return list;
      } else
      {
         struct ero_st *head;

         head = list->next;
         list->next = head->next;
         head->next = list;
         *rightendp = list;
         return head;
      }
   }

   nleft = nlist / 2;
   left  = sort1(list, nleft, &leftend);
   right = sort1(leftend->next, nlist-nleft, &rightend);

   return merge(rightendp, left, leftend, right, rightend);
} /* sort1 */
 void sort1(vector<int>& nums, int i,int j)
 {
     if(i>=j) return ;
     int t=nums[i],start=i,end=j;  // 坑
     while(start<end) {
         while(nums[end]>=t&&end>start) --end;
         if(end>start) nums[start]=nums[end];  // 换坑
         while(nums[start]<t&&end>start) ++start;
         if(end>start) nums[end]=nums[start];
     }
     nums[start]=t;  // 中间量
     sort1(nums,i,start-1);sort1(nums,start+1,j);
 }
Beispiel #4
0
int main ()
{
	vector<double> v(10, 0.0);
	list<double> L;
	for (int i = 0; i < 10; i++)  {
		L.push_back(100 - i * i);
		v.push_back(10-i);
		}
	vectorIterator<double> itr(v);
	for (itr.init(); ! itr; itr++)
	  cout << itr( ) << ' ';
	cout << '\n';
	double total = 0;
	for (itr.init(); ! itr; itr++)
	  total += itr ();
	cout << "total is " << total << '\n';
	vectorIterator<double> jtr(v);
	sort1(itr, jtr);
	for (itr.init(); ! itr; itr++)
	  cout << itr( ) << ' ';
	cout << '\n';
	itr.sort(itr, jtr);
	for (itr.init(); ! itr; itr++)
	  cout << itr( ) << ' ';
	cout << '\n';
	listIterator<double> lt1(L);
	for (lt1.init(); ! lt1; lt1++)
	  cout << lt1( ) << ' ';
	cout << '\n';
	total = 0;
	for (lt1.init(); ! lt1; lt1++)
	  total += lt1 ();
	cout << "total is " << total << '\n';
	listIterator<double> jt1(L);
	sort1(lt1, jt1);
	for (lt1.init(); ! lt1; lt1++)
	  cout << lt1( ) << ' ';
	cout << '\n';
	lt1.sort(lt1, jt1);
	for (lt1.init(); ! lt1; lt1++)
	  cout << lt1( ) << ' ';
	cout << '\n';

   cout << "\nMY STUFF BEGINS HERE!\n";
   cout << "VECTOR ITERATOR:\n" << itr;
   cout << "LIST ITERATOR:\n" << lt1;
   return 1;
}
Beispiel #5
0
int main(){
    omp_set_num_threads(THREADS);
    srand(time(0));

    init_array();
//    print_array(); 

    struct timeval t1, t2;
    double elapsedTime;
    gettimeofday(&t1, NULL);

    sort1();    

    gettimeofday(&t2, NULL);

    elapsedTime = (t2.tv_sec - t1.tv_sec) * 1000.0;      // sec to ms
    elapsedTime += (t2.tv_usec - t1.tv_usec) / 1000.0;   // us to ms
    printf("case 1. data length: %d, threads: %d, time: %gms\n", N, THREADS, elapsedTime);

    init_array();

    gettimeofday(&t1, NULL);

    sort2();    
    
    gettimeofday(&t2, NULL);

    elapsedTime = (t2.tv_sec - t1.tv_sec) * 1000.0;      // sec to ms
    elapsedTime += (t2.tv_usec - t1.tv_usec) / 1000.0;   // us to ms
    printf("case 2. data length: %d, groups: %d, threads: %d, time: %gms\n", N, GROUPS, THREADS, elapsedTime);

    return 0;
}
int main()
{
	int length, index;
	printf("Enter length of array\n");
	scanf("%d", &length);
	int *array, *array1, *array2;
	array = (int*)malloc(100*sizeof(int));
	array1 = (int*)malloc(100*sizeof(int));
	array2 = (int*)malloc(100*sizeof(int));
	for (index = 0; index < length; index++)
	{
		scanf("%d", &array[index]);
		if ((array[index]<0) || (array[index]>1))
		{
			printf("Invalid input-enter either o or 1");
			return 0;
		}
		array1[index] = array[index];
		array2[index] = array[index];

	}
	sort1(array, length);
	for (index = 0; index < length; index++)
		printf("%d\t", array[index]);
	sort2(array1, length);
	for (index = 0; index < length; index++)
		printf("%d\t", array1[index]);
	sort3(array2, length);
	for (index = 0; index < length; index++)
		printf("%d\t", array2[index]);
	getch();
}
Beispiel #7
0
int main()
{
	char a[1005];
	int m,n,i,l,j;	
	__int64 b;
	while(scanf("%s",a)!=EOF)
	{
		m=0;
		l=strlen(a);
		i=0;
		while(a[m]=='5')
			m++;
		for(;m<l;)
		{
			b=0;
			n=0;
			while(a[m]!='5'&&m<l)
			{
				b=b*10+(a[m]-'0');
				m++;
				n=1;
			}
			if(n==1) f[i++]=b;
			m++;
		}
		if(i<=20) sort1(0,i-1);
		else sort2(0,i-1);
		for(j=0;j<i-1;j++)
			printf("%I64d ",f[j]);
		printf("%d\n",f[j]);
	}
	return 0;
}
 int findDuplicate(vector<int>& nums) {
     sort1(nums,0,nums.size()-1);
     for(int i=0;i<nums.size()-1;++i)
         if(nums[i]==nums[i+1])
             return nums[i];
     return 0;
 }
void Sort(int n)
{
 char ch;
 cout<<endl<<endl;
 cout<<"1. Sort accoring to School Code"<<endl;
 cout<<"2. Sort accoring to School Name"<<endl;
 ch=getch();

 if(ch=='1')            sort1(n);
 else                   sort2(n);
}
Beispiel #10
0
void main()
{
int i,n;
int a[10];
printf("enter the size");
scanf("%d",&n);
printf("give arry elements:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
sort1(a,n);

}
void sort1(int l, int r) {
	int i, j, px, py, t;

	if (l < r) {
		i = l; j = r; px = sx[(l + r) / 2]; py = sy1[(l + r) / 2];
		while (i <= j)
			if (sx[i] < px || sx[i] == px && sy1[i] < py)
				i++;
			else if (px < sx[j] || px == sx[j] && py < sy1[j])
				j--;
			else {
				swap(&sx[i], &sx[j]);
				swap(&sy1[i], &sy1[j]);
				swap(&sy2[i], &sy2[j]);
				swap(&sri[i], &sri[j]);
				i++; j--;
			}

		sort1(i, r); sort1(l, j);
	}
}
Beispiel #12
0
int
main (int argc, char **argv)
{
  int i, count = 1000000;
  double stime;
  int *unsorted, *sorted, num_threads;
  if (argc >= 2)
    count = strtoul (argv[1], NULL, 0);

  unsorted = malloc (count * sizeof (int));
  sorted = malloc (count * sizeof (int));
  if (unsorted == NULL || sorted == NULL)
    {
      puts ("allocation failure");
      exit (1);
    }

  srand (0xdeadbeef);
  for (i = 0; i < count; i++)
    unsorted[i] = rand ();

  omp_set_nested (1);
  omp_set_dynamic (0);
  #pragma omp parallel
    #pragma omp single nowait
      num_threads = omp_get_num_threads ();
  printf ("Threads: %d\n", num_threads);

  memcpy (sorted, unsorted, count * sizeof (int));
  stime = omp_get_wtime ();
  sort1 (sorted, count);
  verify ("sort1", stime, sorted, count);

  memcpy (sorted, unsorted, count * sizeof (int));
  stime = omp_get_wtime ();
  sort2 (sorted, count);
  verify ("sort2", stime, sorted, count);

#if _OPENMP >= 200805
  memcpy (sorted, unsorted, count * sizeof (int));
  stime = omp_get_wtime ();
  sort3 (sorted, count);
  verify ("sort3", stime, sorted, count);
#endif

  return 0;
}
Beispiel #13
0
int main(void)
{
    int n, z, rep, wariant;
    t a, b;
    clock_t czas1, czas2;

    srand((unsigned) time(NULL));

    printf("Info INT_MAX: %d\n", INT_MAX);

    do {
        printf("Wariant: (0/1/2) ");
        scanf("%d", &wariant);

        switch (wariant) {
            case 0:
                exit(EXIT_SUCCESS);
            case 1: //sort i sort1
                czyt(&n, &z);
                generacja(n, z, a);
                kopiuj_tablice(n, b, a);
                druk1(n, z, a);
                sort(n, a);
                druk2(n, a);
                sort1(n, b);
                druk2(n, b);
                break;
            case 2: //czas
                czyt1(&n, &z, &rep);
                czas1 = pomiar();
                for (int i = 0; i < rep; i++) {
                    generacja(n, z, a);
                    sort(n, a);
                }
                czas2 = pomiar();

                printf("czas: %lf\n", (double) (czas2 - czas1) / (double) CLOCKS_PER_SEC);
                break;
            default:
                printf("Taki wariant nie istnieje!\n");
        }
    } while (1);
}
Beispiel #14
0
int main(){
	int r,i,originalVector[VECTORSIZE],sortedVector[VECTORSIZE],testVector[VECTORSIZE];
	/*seed random number*/
	srand(1234);
	clock_t start, diff;
	
 /*initialize random number*/
	for (i=0;i<VECTORSIZE;i++)originalVector[i]=rand()%10000;

	/*copy vector to sorted vector*/
	memmove(sortedVector,originalVector,sizeof(originalVector));

 /*insertion sort*/
 start=clock(); /*start timer*/
	insertion_sort(sortedVector,VECTORSIZE); 
 printf ("Insertion sort took %d ms\n",(clock()-start)*1000/CLOCKS_PER_SEC);
 
 /*sort1*/
 /*copy vector to test vector*/
	memmove(testVector,originalVector,sizeof(originalVector));
	start=clock(); /*start timer*/
	sort1(testVector,VECTORSIZE);
	printf ("sort1 took %d ms\n",(clock()-start)*1000/CLOCKS_PER_SEC);
 /*check that it is sorted*/
 if(!compareVectors(sortedVector,testVector,VECTORSIZE)){
		printf("Sort1 failed\n");
		return 0;
	}	

 /*sort2*/
 /*copy vector to test vector*/
	memmove(testVector,originalVector,sizeof(originalVector));
	start=clock(); /*start timer*/
	sort2(testVector,VECTORSIZE);
	printf ("sort2 took %d ms\n",(clock()-start)*1000/CLOCKS_PER_SEC);
 /*check that it is sorted*/
 if(!compareVectors(sortedVector,testVector,VECTORSIZE)){
		printf("Sort2 failed\n");
		return 0 ;
	}
	return 1;
}
Beispiel #15
0
int QuickSort::Sort(Array &array) const
{
	int ret = 0;
	switch (m_type)
	{
	case AlgorithmType_0:
		ret = sort0(array, 0, array.Size() -1);			
		break;
	case AlgorithmType_Lomuto:
		ret = sort1(array, 0, array.Size() -1);	
		break;
	case AlgorithmType_Sedgewick:
		ret = sort2(array, 0, array.Size() -1);	
		break;
	case AlgorithmType_3:
		break;						
	default:
		break;
	}
	return 0;
}
Beispiel #16
0
void main()
{
    int a[100], b[100];
    int n, i, sum = 0;
    scanf("%d", &n);
    for (i = 0; i < n; i++)
    {
        scanf("%d", &a[i]);
        if (a[i]!=0 && a[i]!=1)
        {
            printf("Please Enter only 0's and 1's\n");
            i--;
        }
        else
        {
            sum = sum + a[i];
        }

    }
    sort1(a, n,b);
    for (i = 0; i < n; i++)
    {
        printf("%d", b[i]);
    }
    printf("\n");
    sort2(a, n, b,sum);
    for (i = 0; i < n; i++)
    {
        printf("%d", b[i]);
    }
    printf("\n");
    sort3(a, n);
    for (i = 0; i < n; i++)
    {
        printf("%d", a[i]);
    }
    printf("\n");
    getch();
}
int main()
{
    char array[80], num[80], upper[80], lower[80], temp;
    int i = 0;
    int j = 0;
    int k = 0;
    int l = 0;
    int x, y, z, num1[80];

    scanf("%s", array); 
//    print(array);
    while(array[i] != '\0')
    {
        if((array[i] >= 48) && (array[i] <= 57)) 
        {
            num[j] = array[i];
//           printf("jjjjjjjj%d, %c\n", i, array[i]);
//           printf("eeeeeeee%d, %c\n", j, num[j]);
            j++; 
        }        
        if((array[i] >= 65) && (array[i] <= 90)) 
        {        
           upper[k] = array[i];
//           printf("jjjjjjjj%d, %c\n", i, array[i]);
//           printf("eeeeeeee%d, %c\n", k, upper[k]);
//           i++; 
           k++; 
        }        
        if((array[i] >= 97) && (array[i] <= 122)) 
        {       
           lower[l] = array [i];
//           printf("jjjjjjjj%d, %c\n", i, array[i]);
//           printf("eeeeeeee%d, %c\n", k, lower[l]);
           l++;
//            i++;
        }
        i++;
   }
    x = j;
    y = k;
    z = l;
//    for(j=0; j<=x-1; j++)
//    {
//        printf("num[%d] = %c", j, num[j]); 
//    }
//    sort(num, x); 
//    print(num, x);
    printf("\n");
    sort(upper, y); 
    print(upper, y);
    printf("\n");
    sort(lower, z); 
    print(lower, z);
    printf("\n");

    printf("char to integer.\n");
    for(i=0; i<=x-1; i++)
    {
        num1[i] = num[i] - '0';
//        printf("%d", num1[i]);
    }
    sort1(num1, x); 
    printf("\n");
    print1(num1, x);
    printf("\n");
    return 0;
}
Beispiel #18
0
/*****************************************************************************************
***	Description:		
***		 The main function 
*******************************************************************************************/
int  main()
{
	int i,j;
	int x[N];
	char cmd1,cmd2;
	clock_t begin,end;
	double cost;
	srand((unsigned)time(NULL));
	while(1)
	{
		printf("Please choose sort\nr:random sort,i:increase sort,d:decrease sort,e:equivalent sort,q:quit\n");
		scanf("%c",&cmd1);getchar();
		if(cmd1=='r')
		{
			for(i=0;i<N;i++) 
			{
				x[i]=rand()%5555;
			}	
			show(x,N);
			printf("\n");
		}
		else if(cmd1=='i')
		{
			for(i=0;i<N;i++)
			{
				x[i]=i;
			}
			show(x,N);
			printf("\n");
		}
		else if(cmd1=='d')
		{
			for(i=0,j=N;i<N;i++,j--)
			{
				x[i]=j;
			}
			show(x,N);
			printf("\n");
		}
		else if(cmd1=='e')
		{
			for(i=0;i<N;i++)
			{
				x[i]=5555;
			}
			show(x,N);
			printf("\n");
		}
		else if(cmd1=='q')
		{
			return 0;
		}
		else
		{
			printf("input is error,please check\n")	;
			return 0;
		}

		printf("please input number, 1: choose unoptimized sort or 2: choose optimized sort\n");

		scanf("%c", &cmd2);getchar();
		if(cmd2 == '1')
		{
			begin=clock();
			sort1(x,N);
			end=clock();
			show(x,N);
			printf("\n");
			cost=(double)(end-begin)/CLOCKS_PER_SEC;
			printf("unoptimized sort cost %f seconds\n",cost );
		
		}
	
		else if(cmd2 == '2')
		{	
			begin=clock();
			sort2(x,N);
			end=clock();
			show(x,N);
			printf("\n");
			cost=(double)(end-begin)/CLOCKS_PER_SEC;
			printf("optimized sort cost %f seconds\n",cost );
		}
		else
		{
			printf("input is error,please check\n");
			return 0;
		}
	}
}	
Beispiel #19
0
int main(int argc, char ** argv)
{
	size_t n = DB::parse<size_t>(argv[1]);
	size_t method = DB::parse<size_t>(argv[2]);

	std::vector<Key> data(n);

//	srand(time(0));

	{
		Stopwatch watch;

		for (auto & elem : data)
			elem = rand();

		watch.stop();
		double elapsed = watch.elapsedSeconds();
		std::cerr
			<< "Filled in " << elapsed
			<< " (" << n / elapsed << " elem/sec., "
			<< n * sizeof(Key) / elapsed / 1048576 << " MB/sec.)"
			<< std::endl;
	}

	if (n <= 100)
	{
		std::cerr << std::endl;
		for (const auto & elem : data)
			std::cerr << elem << ' ';
		std::cerr << std::endl;
	}


	{
		Stopwatch watch;

		if (method == 1)	sort1(&data[0], n);
		if (method == 2)	sort2(&data[0], n);
		if (method == 3)	sort3(&data[0], n);

		watch.stop();
		double elapsed = watch.elapsedSeconds();
		std::cerr
			<< "Sorted in " << elapsed
			<< " (" << n / elapsed << " elem/sec., "
			<< n * sizeof(Key) / elapsed / 1048576 << " MB/sec.)"
			<< std::endl;
	}

	{
		Stopwatch watch;

		size_t i = 1;
		while (i < n)
		{
			if (!(data[i - 1] <= data[i]))
				break;
			++i;
		}

		watch.stop();
		double elapsed = watch.elapsedSeconds();
		std::cerr
			<< "Checked in " << elapsed
			<< " (" << n / elapsed << " elem/sec., "
			<< n * sizeof(Key) / elapsed / 1048576 << " MB/sec.)"
			<< std::endl
			<< "Result: " << (i == n ? "Ok." : "Fail!") << std::endl;
	}

	if (n <= 1000)
	{
		std::cerr << std::endl;

		std::cerr << data[0] << ' ';
		for (size_t i = 1; i < n; ++i)
		{
			if (!(data[i - 1] <= data[i]))
				std::cerr << "*** ";
			std::cerr << data[i] << ' ';
		}

		std::cerr << std::endl;
	}

	return 0;
}
Beispiel #20
0
/* Sort $list of $nlist elements. */
static struct ero_st *sort(struct ero_st *list, unsigned nlist)
{
   struct ero_st *tail;
   return sort1(list, nlist, &tail);
} /* sort */
Beispiel #21
0
void xptr_sequence::sort()
{
    sort1(0,seq_size);
}