Skip to content

Instantly share code, notes, and snippets.

@marsharp
Last active December 26, 2015 11:19
Show Gist options
  • Save marsharp/7142472 to your computer and use it in GitHub Desktop.
Save marsharp/7142472 to your computer and use it in GitHub Desktop.
#include <fstream>//qsort by marsharp
#include <iostream>
#include <algorithm>
using namespace std;
int a[100005];
void qsort(int l,int r)
{
int i,j,tmp=a[(r+l)/2];
if(r==l)return;
i=l;
j=r;
while(i<=j)
{
while(a[i]<tmp)i++;
while(a[j]>tmp)j--;
if(i<=j){swap(a[i],a[j]);i++;j--;}
if(i<r)qsort(i,r);
if(l<j)qsort(l,j);
}
}
int main()
{
ifstream cin("input.txt");ofstream cout("output.txt");
int i,n;
cin>>n;
for(i=0;i<n;i++)
cin>>a[i];
qsort(0,n-1);
for(i=0;i<n;i++)
cout<<a[i]<<" ";
return 0;
}
#include <stdio.h> ////qsort by marsharp v1.1
#include <stdlib.h>
#include <time.h>
#include <algorithm>
int a[1658987];
void qsort(int l,int r)
{
int i,j,tmp=a[(r+l)/2];
if(r==l)return;
i=l;
j=r;
while(i<=j)
{
while(a[i]<tmp)i++;
while(a[j]>tmp)j--;
if(i<=j){std::swap(a[i],a[j]);i++;j--;}
}
if(i<r)qsort(i,r);
if(l<j)qsort(l,j);
}
int main()
{
int i,n;
printf("Enter N: ");
scanf("%d",&n);
// int *a=new int[n];
srand (time(NULL));
for(i=0;i<n;i++)
a[i]=rand()%100;
printf("Generated array: \n");
for(i=0;i<n;i++)
printf("%d ",a[i]);
printf("\n");
qsort(0,n-1);
printf("Sorted array: \n");
for(i=0;i<n;i++)
printf("%d ",a[i]);
printf("\n");
return 0;
}
@Rag0n
Copy link

Rag0n commented Dec 21, 2013

Забрал код для курсача, а он не работает. marsharp, что за дела?! )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment