Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class HashTable{
- constructor(cap){
- this.cap= cap
- this.arr= new Array(cap)
- }
- hash(key){
- let index=0
- for(let i=0;i<key.length;i++){
- index+=key.charCodeAt(i)
- }
- return index%=this.cap
- }
- set(key,val){
- const bucket=[key, val ]
- const index= this.hash(key)
- if(Array.isArray(this.arr[index])){
- this.arr[index].push(bucket)
- }else{
- this.arr[index] = [ bucket ]
- }
- }
- get(key){
- const index=this.hash(key)
- if(this.arr[index]===undefined){
- return 'value not found'
- }else{
- return this.arr[index].find( v=> v[0]===key )[1]
- }
- // return console.log(this.arr[index]);
- }
- display(){
- return console.log(this.arr);
- }
- }
- const table= new HashTable(50)
- table.set( 'name', 'Liston' )
- table.set('domain', 'MERN')
- table.set('mane', 'New')
- table.display()
- console.log(table.get('name'));
- console.log(table.get('mane'));
- console.log(table.get('ten'));
- function bubbleSort(a) {
- let swapped, temp;
- do {
- swapped = false;
- for (let i = 0; i < a.length - 1; i++) {
- if (a[i] > a[i + 1]) {
- swapped = true;
- temp = a[i];
- a[i] = a[i + 1];
- a[i + 1] = temp;
- }
- }
- } while (swapped);
- return a
- }
- const a=[10,-2,6,8,2]
- console.log(bubbleSort(a));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement