int main(int argc, char const *argv[]) { struct ListNode* test = NULL; // Push(&test, 44); // Push(&test, 42); // Push(&test, 19); // Push(&test, 35); // Push(&test, 10); // Push(&test, 27); Push(&test, 33); Push(&test, 14); PrintList(test); printf("---after sort\n"); PrintList(insertionSortList(test)); int test1[8] = {14,33,27,10,35,19,42,44}; insertionSortArray(test1, 8); printf("---after sort\n"); for (int i = 0; i < 8; ++i) { printf("%d\n", test1[i]); } return 0; }
int main(void) { LISTA *l = NULL; int listSize = 0; int i = 0; int randVal = 0; int val = 0; clock_t s,f; printf("Informe o tamanho da lista: "); scanf("%i", &listSize); srand(time(NULL)); for(i=0; i<listSize; i++) { randVal = rand()%100; push(&l, randVal); } printf("\n"); printf("Lista desordenada: "); imprimeLista(l); s = clock(); LISTA *ord = insertionSortList(l); f = clock(); printf("\n"); printf("Lista ordenada: "); imprimeLista(ord); float t = (f-s)*1000/CLOCKS_PER_SEC; printf("\nTempo: %.2f\n", t); printf("\n Informe o valor a ser buscado: "); scanf("%i", &val); if(binarySearch(ord, val) == 0) { printf("\n O valor: %i, esta na lista.\n", val); } else { printf("\n Valor nao encontrado!\n"); } listFree(l); listFree(ord); return 0; }
int main(){ struct ListNode *node1, *node2, *node3, *node4; node1 = (struct ListNode *)malloc(sizeof (struct ListNode)); node2 = (struct ListNode *)malloc(sizeof (struct ListNode)); node3 = (struct ListNode *)malloc(sizeof (struct ListNode)); node4 = (struct ListNode *)malloc(sizeof (struct ListNode)); node1->val=4; node1->next=node2; node2->val=3; node2->next=node3; node3->val=2; node3->next=node4; node4->val=1; node4->next=NULL; print(insertionSortList(node3)); return 0; }
int main(int argc, char** argv) { int n = 10; if (argc>1){ n = atoi(argv[1]); } srand(time(NULL)); int *a = new int[n]; for(int i=0; i<n; i++){ a[i] = random()%n + 1; } ListNode *p = createList(a, n); printList(p); printList(insertionSortList(p)); delete[] a; }
/** * @return new head of sorted list */ ListNode *insertionSortList(ListNode *head) { if(head == NULL || head->next == NULL) return head; // sort all nodes after head->next = insertionSortList(head->next); // 1. already sorted, no need to insert if(head->val <= head->next->val) return head; // 2. need to insert, head to return is head->next ListNode *new_head = head->next; // keep track of where to insert ListNode **insert_point = &head->next->next; while(*insert_point && head->val > (*insert_point)->val) insert_point = &(*insert_point)->next; head->next = *insert_point; *insert_point = head; return new_head; }
//-----------recursive insertion sort------------ ListNode *Solution::insertionSortListRecur(ListNode *head) { if (head == NULL || head->next == NULL){ return head; } ListNode *nextHead = insertionSortList(head->next); if (head->val < nextHead->val){ head->next = nextHead; return head; } else{ ListNode *prev = nextHead; while (prev->next != NULL){ if (head->val < prev->next->val){ head->next = prev->next; prev->next = head; return nextHead; } prev = prev->next; } prev->next = head; head->next = NULL; return nextHead; } }