Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. mergesort

mergesort

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++sharepointgraphics
1 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Y Offline
    Y Offline
    Ylis
    wrote on last edited by
    #1

    I'm trying to implement mergesort in C++ but I've grown blind to my code and can't seem to spot the error. It sorts my vector partly, but not entirely. Anyway here's the relevant code: void merge_sort( std::vector &d, int lb, int ub ) { if( lb < ub ) { int q = ( lb + ub ) / 2; merge_sort( d, lb, q ); merge_sort( d, q + 1, ub ); merge( d, lb, q, ub ); } } void merge( std::vector &d, int lb, int sp, int ub ) { int l1 = sp - lb; int l2 = ub - sp; std::vector vL(d.begin() + lb ,d.begin() + lb + l1); std::vector vR(d.begin() + sp , d.begin() + sp + l2); vL.push_back(INT_MAX); vR.push_back(INT_MAX); int i = 0; int j = 0; for( int k = lb; k < ub; k++ ) { if( vL[i] < vR[j] ) d[k] = vL[i++]; else d[k] = vR[j++]; } } Any help appriciated :)

    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups