DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Data Engineering
  3. AI/ML
  4. Sorting Algorithms

Sorting Algorithms

Snippets Manager user avatar by
Snippets Manager
·
Apr. 28, 06 · Code Snippet
Like (0)
Save
Tweet
Share
2.13K Views

Join the DZone community and get the full member experience.

Join For Free
This code uses a variety of sorting methods. This code compiles with Borland C++ 5.0 it's useful for seeing how to duplicate the algorithms in other languages. Each sort is found in a separate function called in the main function. This version uses a vector and can sort up to 5 million numbers(possibly more). I suggest larger vectors use the quicksort, unless you want to wait a few days. I find the code fairly concise it makes random numbers depending on the amount you specify for sorting and then outputs the time in seconds to sort the number vector. 

It uses an option menu and makes use of 
-Bubble Sort
-Selection Sort
-Insertion Sort
-Shell Sort
-Quick Sort  


#include "iostream.h"
#include "stdlib.h"
#include "time.h"
#include "conio.h"
#include "vector.h"
using namespace std;
// VIEW RESULTS
void view_sort(vector  arr, long max)
{
    for(long x=0;x bubble(vector  arr, long max){
int tmp;
for(long i=0;i arr[x+1])
    {
    //r.push_back(rnd);
   tmp = arr[x];
   arr[x] = arr[x+1];
   arr[x+1] = tmp;
  }
 }

}
return  arr;
}
// SELECTION SORT
vector  select(vector  arr, long max){
int tmp;
int min;
for(long i=0;i insert(vector  arr, long max)
{
int i, j, index;
for(i=1; i < max;i++)
{
    index  = arr[i];
    j = i;
    while((j > 0) && (arr[j-1] > index))
       {
        arr[j] = arr[j-1];
        j = j-1;
       }
   arr[j] = index;
}
return arr;
}
// SHELL SORT
vector  shell(vector  arr, long max){

int index, i, j;
for(i=1; i < max;i++)
{
    index  = arr[i];
    j = i;
    while((j > 0) && (arr[j-1] > index))
       {
        arr[j] = arr[j-1];
        j = j-1;

      }
   arr[j] = index;
}
return arr;
}
/* ######## QUICK SORT ######### */
void quicksort(vector  & arr, int start, int end){
int pivot, starth, endh; // store pivot # keep start & end in memory for split
starth = start;
endh = end;
pivot = arr[start];

while(start < end)
{
 while((arr[end] >= pivot) && (start < end))
  end--;
    if (start != end)
    {
      arr[start] = arr[end];
      start++;
    }
  while ((arr[start] <= pivot) && (start < end))
      start++;
    if (start != end)
    {
      arr[end] = arr[start];
      end--;
    }
}
arr[start] = pivot;
pivot = start;
start = starth;
end = endh;
if(start < pivot)
quicksort(arr, start, pivot-1);
if(end > pivot)
quicksort(arr, pivot+1, end);
}

/* BEGIN MAIN */
int main(){
time_t start;
long rnd = rand()%1000+1;
vector  r;
//long r[200001];
long tmp;
long maxnum; // if you overload this variable, beware!
int option;
bool stop = false;
bool view = false;
while(!stop){
cout<<"\t\t\tAlgorithms For Sorting Numbers\n"<>option;

// ARRANGE ARRAY
cout<<"Length (<=5000000 )\n";
cin>>maxnum;

if(maxnum == 1)
maxnum = 1000000;

for(long x=0;x>v;
if(v == 1)
{
view = true;
view_sort(r,maxnum);
}
// end program?
cout<>s;
if(s == 0)
stop = true; clrscr();
}
}
Algorithm Sorting

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Kubernetes-Native Development With Quarkus and Eclipse JKube
  • How To Perform Local Website Testing Using Selenium And Java
  • 7 Ways for Better Collaboration Among Your Testers and Developers
  • ClickHouse: A Blazingly Fast DBMS With Full SQL Join Support

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: