ascending and descending algorithm tested