Advertisement
ListonFermi

Week 14 2nd review

Mar 20th, 2024
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 1.45 KB | Source Code | 0 0
  1. class HashTable{
  2.     constructor(cap){
  3.         this.cap= cap
  4.         this.arr= new Array(cap)
  5.     }
  6.  
  7.     hash(key){
  8.         let index=0
  9.         for(let i=0;i<key.length;i++){
  10.             index+=key.charCodeAt(i)
  11.         }
  12.         return index%=this.cap
  13.     }
  14.  
  15.     set(key,val){
  16.         const bucket=[key, val ]
  17.         const index= this.hash(key)
  18.  
  19.         if(Array.isArray(this.arr[index])){
  20.             this.arr[index].push(bucket)
  21.         }else{
  22.             this.arr[index] = [ bucket ]
  23.         }
  24.     }
  25.  
  26.     get(key){
  27.         const index=this.hash(key)
  28.  
  29.         if(this.arr[index]===undefined){
  30.             return 'value not found'
  31.         }else{
  32.             return this.arr[index].find( v=> v[0]===key  )[1]
  33.         }
  34.  
  35.         // return console.log(this.arr[index]);
  36.     }
  37.  
  38.     display(){
  39.         return console.log(this.arr);
  40.     }
  41. }
  42.  
  43. const table= new HashTable(50)
  44. table.set( 'name', 'Liston'  )
  45. table.set('domain', 'MERN')
  46. table.set('mane', 'New')
  47. table.display()
  48. console.log(table.get('name'));
  49. console.log(table.get('mane'));
  50. console.log(table.get('ten'));
  51.  
  52.  
  53.  
  54. function bubbleSort(a) {
  55.   let swapped, temp;
  56.   do {
  57.     swapped = false;
  58.     for (let i = 0; i < a.length - 1; i++) {
  59.       if (a[i] > a[i + 1]) {
  60.         swapped = true;
  61.  
  62.         temp = a[i];
  63.         a[i] = a[i + 1];
  64.         a[i + 1] = temp;
  65.       }
  66.     }
  67.   } while (swapped);
  68.  
  69.   return a
  70. }
  71.  
  72. const a=[10,-2,6,8,2]
  73. console.log(bubbleSort(a));
  74.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement