Changeset f2cdc44 for src


Ignore:
Timestamp:
Apr 2, 2017, 12:20:37 PM (8 years ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
Children:
0788c03d, ee89ad43
Parents:
06cf47f
Message:

more alternate bsearch returning posn of key

Location:
src/tests
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • src/tests/.expect/searchsort.txt

    r06cf47f rf2cdc44  
    1110, 9, 8, 7, 6, 5, 4, 3, 2, 1,
     2
    231, 2, 3, 4, 5, 6, 7, 8, 9, 10,
     410, 9, 8, 7, 6, 5, 4, 3, 2, 1,
    3510, 9, 8, 7, 6, 5, 4, 3, 2, 1,
    4610, 9, 8, 7, 6, 5, 4, 3, 2, 1,
  • src/tests/searchsort.c

    r06cf47f rf2cdc44  
    1010// Created On       : Thu Feb  4 18:17:50 2016
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Sat Apr  1 19:10:12 2017
    13 // Update Count     : 62
     12// Last Modified On : Sun Apr  2 11:29:30 2017
     13// Update Count     : 76
    1414//
    1515
    1616#include <fstream>
    1717#include <stdlib>                                                                               // bsearch, qsort
     18#include <stdlib.h>                                                                             // C version of bsearch
     19
     20int comp( const void * t1, const void * t2 ) { return *(int *)t1 < *(int *)t2 ? -1 : *(int *)t2 < *(int *)t1 ? 1 : 0; }
    1821
    1922int main( void ) {
     
    2528                sout | iarr[i] | ", ";
    2629        } // for
    27         sout | endl;
     30        sout | endl | endl;
     31
     32        // ascending sort/search by changing < to >
    2833        qsort( iarr, size );
    2934        for ( unsigned int i = 0; i < size; i += 1 ) {
    3035                sout | iarr[i] | ", ";
     36        } // for
     37        sout | endl;
     38        for ( unsigned int i = 0; i < size; i += 1 ) {          // C version
     39                int key = size - i;
     40                int *v = bsearch( &key, iarr, size, sizeof( iarr[0] ), comp );
     41                sout | *v | ", ";
    3142        } // for
    3243        sout | endl;
Note: See TracChangeset for help on using the changeset viewer.